悬关求助大佬!60求助!

P1478 陶陶摘苹果(升级版)

andy605 @ 2023-10-31 21:02:10

#include <bits/stdc++.h>
using namespace std;
struct app{
    int x,y;
};
bool cmp(app a,app b){
    return (a.y<=b.y);
}
int s,n,b,a,num;
int main(){
    cin>>n>>s;
    cin>>a>>b;
    app apple[5000];
    for(int i=0;i<n;i++){
        cin>>apple[i].x>>apple[i].y;
    }
    sort(apple,apple+n,cmp);
    for(int i=0;i<n;i++){
        if(s>apple[i].y){
            if(apple[i].x<=(a+b)){
                num++;
                s-=apple[i].y;
            }
        }
    }
    cout<<num<<endl;
    return 0;
}

这道题用dev测试完全可行,在洛谷上确是:

1WA #2AC #3RE #4AC #5AC

到底是哪里出了问题?悬关求助!


by BGM114514 @ 2023-10-31 21:26:03

if(s>apple[i].y)

应改成

if(s>=apple[i].y)

by BGM114514 @ 2023-10-31 21:30:30

不知为什么,把sort换成stable_sort就过了


by BGM114514 @ 2023-10-31 21:31:39

AC代码:

#include <bits/stdc++.h>
using namespace std;
struct app{
    int x,y;
    app():x(0),y(0){}
};
bool cmp(app a,app b){
    return a.y<=b.y;
}
int s,n,b,a,num;
app apple[5005];
int main(){
    cin>>n>>s;
    cin>>a>>b;
    for(int i=1;i<=n;i++){
        cin>>apple[i].x>>apple[i].y;
    }
    stable_sort(apple+1,apple+n+1,cmp);
    for(int i=1;i<=n;i++){
        if(s>=apple[i].y){
            if(apple[i].x<=a+b){
                num++;
                s-=apple[i].y;
            }
        }
    }
    cout<<num<<endl;
    return 0;
}

by andy605 @ 2023-11-05 09:26:42

@BGM114514 AC了,谢谢大佬


by andy605 @ 2023-11-05 09:27:14

@BGM114514 已关,谢谢


|