60分求助

P1478 陶陶摘苹果(升级版)

114514_10086 @ 2023-05-27 20:50:37

#include<bits/stdc++.h>
using namespace std;
struct taotao{
    int x,y;
};
taotao a[10010];
int n,s,b,c,ans;
bool cmp(taotao x,taotao y)
{
    if(x.y<y.y) return 1;
    if(x.y>y.y) return 0;
}
int main()
{
    cin>>n>>s>>b>>c;
    c+=b;
    for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y;
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        if(a[i].x<=c && s>=a[i].y)
        {
            ans+=1;
            s-=a[i].y;
        }
    }
    cout<<ans;
    return 0;
}

如上,WA了#3和#5


by A6n6d6y6 @ 2023-05-27 21:00:48

将cmp函数改为

bool cmp(taotao x,taotao y){
    return x.y<y.y;
}

by 114514_10086 @ 2023-05-27 21:13:28

过了!谢谢大佬!!!


by jiangjinke @ 2023-06-14 16:48:52

#include<bits/stdc++.h>

using namespace std;
struct No {
    int x;
    int y;
};
bool Nog(No c,No d) {
    return c.y<d.y;
}
int main() {
    int n,s,a,b,sum=0;
    cin>>n>>s>>a>>b;
    int m=a+b;
    No ar[n];
    for(int i=0; i<n; i++) {
        cin>>ar[i].x>>ar[i].y;
    }
    sort(ar,ar+n,Nog);
    for(int i=0; i<n; i++) {
        if(ar[i].x<=m&&ar[i].y<=s) {
            s-=ar[i].y;
            sum++;
        }
    }
    cout<<sum;
    return 0;
}

|