20分求调

P1478 陶陶摘苹果(升级版)

TipherethB @ 2024-09-14 20:22:07

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

struct node {
    int h;
    int w;
}apple[5010];

int n, s, a, b, num;

bool cmp(node x, node y) {
    return x.w < y.w;
}

int main() {
    scanf("%d%d", &n, &s);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &apple[i].h);
        scanf("%d", &apple[i].w);
    }
    sort(apple+1, apple+1+n, cmp);
    for (int i = 1; i <= n; i++) {
        if (a + b >= apple[i].h && s >= apple[i].w) {
            s-=apple[i].w;
            num+=1;
        }
    }
    printf("%d", num);
}

by PluviaLaver @ 2024-09-14 20:29:01

你最好有读入 a 和 b。


by chenxu11 @ 2024-09-14 20:49:51

AC代码

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

@TipherethB 求关


by TipherethB @ 2024-09-15 08:07:48

@chenxu11 谢谢已关


|