求助大佬,80分

P1478 陶陶摘苹果(升级版)

qqqevinxxz @ 2019-08-30 16:50:17

#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
struct apple
{
    int x,y;
}ap[5050];
bool cmp(apple a,apple b)
{
    if(a.y<b.y) return 1;
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    int n,s,a,b;
    cin>>n>>s>>a>>b;
    int can=0,sum=a+b;
    for(int i=1;i<=n;i++)
    {
        int qq,qqq;
        cin>>qq>>qqq;
        if(qq<=sum) {
            can++;
            ap[can].x=qq;
            ap[can].y=qqq;
        }
    }
    sort(ap+1,ap+can+1,cmp);
    int res=s,ans=0;
    for(int i=1;i<=can;i++)
    {
        if(res<=ap[i].y) break;
        ans++;
        res-=ap[i].y;
    }
    cout<<ans<<endl;
    return 0;
}

by zhanghengrui @ 2019-08-30 17:08:28

@Genoa21 把你原来代码里面的if(res<=ap[i].y) break;改成if(res<ap[i].y) break;就可以了


by qqqevinxxz @ 2019-08-31 10:38:57

@zhanghengrui 谢谢


|