20分求大佬解答

P1478 陶陶摘苹果(升级版)

estar_17k @ 2024-03-09 10:48:08

#include<iostream>
#include<algorithm>
using namespace std;
struct pinguo
{
    int m,n;
}a[5005];
bool comp(pinguo a,pinguo b){
    return a.n>b.n;
}
int main()
{
    int shu,li,yizi,shou,x=0;
    cin>>shu>>li>>yizi>>shou;
    for(int i=0;i<shu;i++)cin>>a[i].m>>a[i].n;
    sort(a,a+1,comp);
    for(int i=0;i<shu;i++)
        if(li>=a[i].n && yizi+shou>=a[i].m){
            x++;
            li-=a[i].n;
        }
    cout<<x;
    return 0;
}

by estar_17k @ 2024-03-09 10:49:56

在线等,急


by __Rickysun__ @ 2024-03-09 11:16:01

@estar_17k

两个问题:

```cpp bool cmp(Node u,Node v){ if(u.y==v.y) return u.x<v.x; return u.y<v.y; } ``` 可以排序一下椅子高度,这样到时候,可以直接 $break 你的代码修改后的 $AC$ 代码: ```cpp #include<iostream> #include<algorithm> using namespace std; struct pinguo { int m,n; }a[5005]; bool comp(pinguo a,pinguo b){ return a.n>b.n; } int main() { int shu,li,yizi,shou,x=0; cin>>shu>>li>>yizi>>shou; for(int i=0;i<shu;i++)cin>>a[i].m>>a[i].n; sort(a,a+1,comp); for(int i=0;i<shu;i++) if(li>=a[i].n && yizi+shou>=a[i].m){ x++; li-=a[i].n; } cout<<x; return 0; } ``` 我的代码(可以参考一下贪心优化,不过我用的都是格式化读入输出): ```cpp #include<bits/stdc++.h> using namespace std; int n,m,a,b,sum,ans; struct Node{ int x,y; }apple[5001]; bool cmp(Node u,Node v){ if(u.y==v.y) return u.x<v.x; return u.y<v.y; } int main() { scanf("%d%d%d%d",&n,&m,&a,&b); sum=a+b; for(int i=1;i<=n;i++) scanf("%d%d",&apple[i].x,&apple[i].y); sort(apple+1,apple+n+1,cmp); for(int i=1;i<=n;i++){ if(m>=apple[i].y){ if(sum>=apple[i].x){ ans++; m-=apple[i].y; } }else break; } printf("%d",ans); return 0;//qwq } ``` **~~蒟蒻求关注~~**

by __Rickysun__ @ 2024-03-09 11:17:22

@estar_17k 不对,你带代码修改后的 AC 代码帖错了,应该是这个:

#include<iostream>
#include<algorithm>
using namespace std;
struct pinguo
{
    int m,n;
}a[5005];
bool comp(pinguo a,pinguo b){
    return a.n<b.n;
}
int main()
{
    int shu,li,yizi,shou,x=0;
    cin>>shu>>li>>yizi>>shou;
    for(int i=0;i<shu;i++)cin>>a[i].m>>a[i].n;
    sort(a,a+shu,comp);
    for(int i=0;i<shu;i++)
        if(li>=a[i].n && yizi+shou>=a[i].m){
            x++;
            li-=a[i].n;
        }
    cout<<x;
    return 0;
}

不小心把你的错误代码贴上去了


by estar_17k @ 2024-03-09 17:39:43

@Rickysun 谢大佬,已关注!!!


|