球球各位大佬帮我看看吧,心态已经崩了

P1478 陶陶摘苹果(升级版)

williaw @ 2021-02-02 17:40:50

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
struct ab{
    int h,f;
}s[100000];
bool cmp(ab a,ab b){
    if(a.f<b.f) return true;
    return false;
}
int main(){
    int n,x,a,b;
    cin>>n>>x>>a>>b;
    int len=a+b;
    int tot=0;
    for(int i=1;i<=n;i++){
        int temp1,temp2;
        cin>>temp1>>temp2;
        if(temp1<=len){
            tot++;
            s[tot].h=temp1;
            s[tot].f=temp2;

        }
    }
    sort(s+1,s+1+tot,cmp);
    int ans=0;
    for(int i=1;i<=tot;i++){
        x-=s[i].f;
        ans++;
        if(x<=0) break;
    }
    cout<<ans;
    return 0;
}

by luo_shen @ 2021-02-02 17:58:07

要先判x<=0,再ans++


by InBlue @ 2021-02-02 18:02:15

还有,花费的力气可能为0


by InBlue @ 2021-02-02 18:03:50

您的代码:

    for(int i=1;i<=tot;i++){
        x-=s[i].f;
        ans++;
        if(x<=0) break;
    }

修改后:

    for(int i=1;i<=tot;i++){
        x-=s[i].f;
        if(x<0) break;
        ans++;
    }

by williaw @ 2021-02-02 20:15:36

谢谢,已经过了


|