80分求助

P1478 陶陶摘苹果(升级版)

金城武 @ 2020-07-01 08:21:39

#include <bits/stdc++.h>
using namespace std;
int sum;
struct apple
{
    int x;
    int y;
}ap[5005];
bool cmp(apple p,apple q)
{
    return p.y<q.y;
}
int main ()
{
    int n,s;
    int a,b;
    cin>>n>>s;
    cin>>a>>b;
    int len=a+b; 
    for (int i=1;i<=n;i++)
    {
        cin>>ap[i].x>>ap[i].y;
    }
    sort(ap+1,ap+n+1,cmp);
    for (int i=1;i<=n;i++)
    {
        if (len>=ap[i].x)
        {
            if (ap[i].y<s)
            {
                sum++;
                s-=ap[i].y;
            }
        }
        if (s<=0)
        break;
    }
    cout<<sum;
 } 

by 咖啡味格子 @ 2020-09-23 16:56:19

摘的时候不能按输入的顺序吧,应该按摘的力气从小到大摘,不然应该会有差


|