#2 #5WA 贪心思路

P1478 陶陶摘苹果(升级版)

_weishiqi66_ @ 2022-11-04 13:58:31

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

by 晴空一鹤 @ 2022-11-04 14:12:59

@weishiqi66 改为if(x<=a+b||x==0) 即可,另外记得删调试语句


|