求大佬优化

P1478 陶陶摘苹果(升级版)

w_moon @ 2024-04-17 21:54:55

已过,求大佬给我优化优化(跪求)

#include<bits/stdc++.h>
using namespace std;

//O(3n)
int n,s,h,t,ans;    //这回清楚多了,还是结构体更好使
struct node {   //结构体存储
    int x,y;
} a[5005];
bool cmp(node a,node b) {
    return a.y<b.y;
}
int main()
{
    cin>>n>>s>>h>>t;
    int len=h+t;
    for(int i=1;i<=n;i++) {
        cin>>a[i].x>>a[i].y;
    }
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        if(len>=a[i].x) {
            s-=a[i].y;
            if(s<0) {
                break;
            }
            ans++;
        }
    }
    cout<<ans;
    return 0;   //优雅地结束程序
}

----这是分割线-------- 谢谢大佬!!!


by d15895997996x @ 2024-07-26 20:23:05

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

|