为什么超时

P1478 陶陶摘苹果(升级版)

Da_La_Jiao @ 2024-07-08 11:57:58

#include <bits/stdc++.h>

using namespace std;

struct node{
    int x,y;
}

tr[5005];

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

int main() {
    int n,s,a,b,cnt=0;
    cin>>n>>s>>a>>b;
    for(int i=1;i<=n;i++){
        cin>>tr[i].x>>tr[i].y;
    }
    sort(tr+1,tr+n-1,cmp);
    for(int i=1;i<=n&&s>=0;i++){
        if(a+b>=tr[i].x&&s>=tr[i].y){
            cnt++;
            s-=tr[i].y;
        }
    }
    cout<<cnt;
    return 0;
}

为什么第四个点运行超时


by dengchengyu @ 2024-07-08 12:15:24

sort(tr+1,tr+n-1,cmp);

改为

sort(tr+1,tr+n+1,cmp);

by Da_La_Jiao @ 2024-07-09 09:16:22

谢谢


|