20分求助

P1478 陶陶摘苹果(升级版)

Alannix @ 2023-03-17 17:29:50

#include<bits/stdc++.h>
using namespace std;
struct s
{
    long long x,y,c;
}su[10000009];
bool cmp(s a,s b)
{
    a.c<b.c;
}
int main()
{
    long long n,s,a,b,sum=0;
    cin>>n>>s;
    cin>>a>>b;
    for(int i=0;i<n;i++){
    cin>>su[i].x>>su[i].y;
    su[i].c=su[i].x/su[i].y;
    }
    sort(su,su+n,cmp);
    for(int i=0;i<n;i++)
    {
        if((a+b)<=su[i].x){
        if(s>=0){
        s-=su[i].y;
        sum++;
    }
}
    else 
    break;
    }
    cout<<sum;
    return 0;
}

by youjiarui @ 2023-03-18 13:55:24

#include<bits/stdc++.h>
using namespace std;
int n,s,a,b,sum=0,x[5001],y[5001];
void px(int i){
    for(int i=0;i<n;i++){
        cin>>x[i]>>y[i];
        for(int j=0;j<=i;j++)
            if(x[j]<x[i]) swap(x[j],x[i]),swap(y[j],y[i]);
    }
    return;
}
int main(){
    cin>>n>>s;
    cin>>a>>b;
    a+=b;
    px(0);
    for(int i=0;i<n;i++){
        if(x[i]<=a)
            if(y[i]<=s)
                s-=y[i],sum++;
    }
    cout<<sum;
    return 0;
}

修改了亿点点,没提交,应该可以AC


|