求助

P1478 陶陶摘苹果(升级版)

lan1812 @ 2024-07-24 08:10:11

#include<iostream>
#include<algorithm>
using namespace std;
struct ap{
    int h,s;
}a[5005];
bool cmp(ap d,ap b)
{
    return d.s<b.s;
}
int main()
{
    int n,s,c,b,num,sum=0;
    cin>>n>>s>>c>>b;
    num=c+b;
    for(int i=0;i<n;i++)
    {
        cin>>a[i].h>>a[i].s;
    }
    sort(a,a+n,cmp);
    int i=0;
    while(1)
    {
        if(a[i].h>num)
        {
            i++;
            continue;
        }
        else if(s >=a[i].s){
            s-=a[i].s;
            sum++;
        }
        i++;
        if(s<=0|| i == n-1)break;

    }
    cout<<sum;
    return 0;
}

为什么第4个测试点过不去?


by LOBA @ 2024-07-24 08:45:54

有可能数组开小了


by LBlue @ 2024-07-24 15:14:37

#include<iostream>
#include<algorithm>
using namespace std;
struct ap{
    int h,s;
}a[5005];
bool cmp(ap d,ap b)
{
    return d.s<b.s;
}
int main()
{
    int n,s,c,b,num,sum=0;
    cin>>n>>s>>c>>b;
    num=c+b;
    for(int i=0;i<n;i++)
    {
        cin>>a[i].h>>a[i].s;
    }
    sort(a,a+n,cmp);
    for(int i = 0;i < n;i++){//把while循环改成for循环
            if(a[i].h > c+b) continue;
            if(a[i].s > s) continue;
            sum++;
            s -= a[i].s;
        }
    cout<<sum;
    return 0;
}

帮你试过了,能过


|