3re 1wa 1ac 求助

P1478 陶陶摘苹果(升级版)

O_OKevinG114 @ 2022-07-08 11:43:24

#include<bits/stdc++.h>
using namespace std;
struct a{
    int gd;
    int lq;
}apple[1000];
bool comp(struct a x,struct a y){
    return x.lq>y.lq;
}
int main(){
    int n,s,a,b,ans=0;
    cin>>n>>s;
    cin>>a>>b;
    for(int i=0;i<n;i++){
        cin>>apple[i].gd;
        cin>>apple[i].lq;
    }sort(apple,apple+n,comp);
    for(int i=0;i<n;i++){
        if(s-apple[i].lq>0){
            if(apple[i].gd<=b||apple[i].gd<=a+b){
                ans++;
                s-=apple[i].lq;
            }
        }
    }cout<<ans;
    return 0;
}

by O_OKevinG114 @ 2022-07-08 11:44:24

说错了 是3WA,1RE,1AC


by ImposterAnYu @ 2022-07-08 11:51:41

@gkw_123_bc

  1. 因为最后的力气 s 可以等于 0,所以

        if(s-apple[i].lq>0)

    应该为

        if(s-apple[i].lq>=0)


by ImposterAnYu @ 2022-07-08 11:53:11

还有个小优化:

            if(apple[i].gd<=b||apple[i].gd<=a+b)

可以直接写成

            if(apple[i].gd<=a+b)

,一样不会错。


by Otue @ 2022-07-08 13:25:02

不是,这道题贪心是要尽量选力气小的苹果啊。。。

bool comp(struct a x,struct a y){
    return x.lq>y.lq;
}

应该改为:

bool comp(struct a x,struct a y){
    return x.lq<y.lq;
}

by Otue @ 2022-07-08 13:27:36

参考一下我的代码:

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

const int N = 5005;
int n, s, a, b, res;

struct stu {
    int x, y; 
}ed[N];

bool cmp(stu x, stu y) { return x.y < y.y; }   //从小到大排序

int main() {
    cin >> n >> s;
    cin >> a >> b;
    b += a;  //b 代表可以摘得最高高度
    for (int i = 1; i <= n; i++) {
        cin >> ed[i].x >> ed[i].y;
    }
    sort(ed + 1, ed + n + 1, cmp);
    for (int i = 1; i <= n; i++) {
        if (s - ed[i].y >= 0) {  //若力气足够
            if (ed[i].x <= b) {  //且能摘得到
                s -= ed[i].y;
                res++;
            }
        } 
        else break; //力气不够了,退出
    }
    cout << res << endl;
}  

by Otue @ 2022-07-08 13:28:09

@gkw_123_bc


by Otue @ 2022-07-08 13:30:09

@owo_ImposterAnYu_owo 您好像还有错误没有找到...


by ImposterAnYu @ 2022-07-08 13:33:57

@君临征天下 呃……没细看代码


by O_OKevinG114 @ 2022-07-08 13:42:48

@owo_ImposterAnYu_owo 谢谢,早就ac了


by O_OKevinG114 @ 2022-07-08 13:43:10

@君临征天下 谢谢,早就ac了


|