心态炸裂

P1478 陶陶摘苹果(升级版)

_短笛无腔_ @ 2021-08-20 08:52:43

#include<bits/stdc++.h>
using namespace std;
int ap[5005],sp[5005],cn[5005];
bool cmp(int a,int b){
    return a<b;
}
int main(){
    int n,s;
    cin>>n>>s;
    int a,b;
    cin>>a>>b;
    int tmp=1;
    for(int i=1;i<=n;i++){
        cin>>ap[i]>>sp[i];
        if(ap[i]<a+b){
            cn[tmp]=sp[i];
            tmp++;
        }
    }
    sort(cn+1,cn+tmp+1,cmp);
    int cann=0;
    for(int i=1;s-cn[i]>=0;i++){
        s-=cn[i];
        cann++;
    }
    cout<<cann;
    return 0;
}

测样例的时候得数是5(应该为4),中间输出竟然把200 0这一个苹果算上了,怎么看都不可能算上啊!

心态炸裂……


by operator_ @ 2021-08-20 09:05:34

sort(cn+1,cn+tmp,cmp); 试试


by operator_ @ 2021-08-20 09:06:28

tmp本来就比实际数量多1


by 035966_L3 @ 2021-08-20 09:08:02

还有,if(ap[i]<=a+b)……


by 035966_L3 @ 2021-08-20 09:13:26

再有,

6 10
10 10
10 1
15 2
20 3
25 2
30 1
35 2

这时最后一次循环会RE!(力气用不完)


by 035966_L3 @ 2021-08-20 09:19:16

建议sort前一行加一个cn[tmp]=INT_MAX(这样sort也不用改了)


by operator_ @ 2021-08-20 09:24:45

@035966_L3 牛哇


|