求救!!!!

P1478 陶陶摘苹果(升级版)

liuxinyi001 @ 2024-05-30 18:14:38

求救

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,s,a,b,c[5002],d[5002],nx=0,e[5001]={0},num=0;
    cin>>n>>s>>a>>b;
    for(int i=1;i<=n;i++){
        cin>>c[i]>>d[i];
    }
    for(int i=1;i<=n;i++){
        if(c[i]<=(a+b)){
            if(d[i]>=nx){
                e[i]=d[i];
            }
        }
    }
    for(int i=1;i<=n;i++){
        if(e[i]+e[i+1]<=s){
            num++;
        }
        else{
            cout<<num;
        }
    }
    return 0;
}

by Entity_XD @ 2024-05-30 18:26:50

用struct,sort排序


by Entity_XD @ 2024-05-30 18:50:44

给你改了一下

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

求关


|