为啥RE啊???

P1478 陶陶摘苹果(升级版)

MC_OIer @ 2023-09-06 14:29:00

#include<bits/stdc++.h>
using namespace std;
struct node{
    int h,l;
}c[5005];
bool cmp(node a,node b){
    return a.l<=b.l;
}
int main(){
    int n,s,a,b,ans=0;
    cin>>n>>s>>a>>b;
    for(int i=1;i<=n;i++)cin>>c[i].h>>c[i].l;
    sort(c+1,c+n+1,cmp);
    for(int i=1;i<=n;i++){
        if(a+b>=c[i].h&&s>=c[i].l){
            s-=c[i].l;
            ans++;
        }
        else if(s<c[i].l)break;
    }
    cout<<ans;
    return 0;
}

by MC_OIer @ 2023-09-06 14:29:52

RE记录


by 幻想繁星 @ 2023-09-06 14:31:21

@linhaoyu2005 sort函数需要保证比较唯一性


by 幻想繁星 @ 2023-09-06 14:31:36

bool cmp(node a,node b){
    return a.l<b.l;
}

by MC_OIer @ 2023-09-06 14:36:21

虽然听不懂,但大受震撼


by MC_OIer @ 2023-09-06 14:37:34

谢谢dalao,但没听懂为啥


by littleqwq @ 2023-09-06 21:52:36

@幻想繁星 这是为什么?


by GUANJUNHOU @ 2023-09-19 18:15:45

sort函数是快速排序

它会来回比较

等于号可能会让它死循环吧 狗头


|