60求助QAQ!2,4点RE,不知道怎么改(哭

P1478 陶陶摘苹果(升级版)

Gray_4 @ 2022-04-06 08:17:59

#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
struct info{
    int H,S;
}apple[5010];

bool cmp1(info a,info b)
{
    return a.H<b.H;
}
bool cmp2(info a,info b)
{
    return a.S<b.S;
}

int main()
{
    int n,i,s,a,b;
    scanf("%d%d%d%d",&n,&s,&a,&b);
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&apple[i].H,&apple[i].S);
    }
    sort(apple,apple+n,cmp1);
    for(i=0;apple[i].H<=(a+b);i++);
    sort(apple,apple+i,cmp2);
    i=0;
    while(s>=0&&i<n)
    {
        s-=apple[i].S;
        i++;
    }
    printf("%d",i-1);
    return 0;
}

by LJ07 @ 2022-04-06 08:24:20

@Gray_4 for(i=0;apple[i].H<=(a+b);i++);改成for(i=0;i<n&&apple[i].H<=(a+b);i++);试试


by Gray_4 @ 2022-04-06 08:43:45

@LJ07 阿里嘎多!刚刚也发现了这个问题,感谢!


|