60分求助!第三个点和第五个点WA

P1478 陶陶摘苹果(升级版)

danaqi_ @ 2023-08-22 09:23:03

请求大佬帮调

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int n,s,a,b,sum;
struct add{
    int x,y;
}ans[5005];
int main(){
    scanf("%d%d%d%d",&n,&s,&a,&b);
    for(int i=1;i<=n;++i)
        scanf("%d%d",&ans[i].x,&ans[i].y); 
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n-i;++j){
            if(ans[j].y>ans[j+1].y)
                swap(ans[j],ans[j+1]);
        }
    } 
    int i=1;
    while(s-ans[i].y>=0&&i<=n){
        if(b+a>=ans[i].x)
            ++sum;
        s-=ans[i].y;
        ++i;
    }
    printf("%d",sum);
    return 0;
} 

by yanlikun @ 2023-08-22 09:31:20

 if(b+a>=ans[i].x)
    ++sum;
 s-=ans[i].y;

应改为

if(b+a>=ans[i].x){
   ++sum;
   s-=ans[i].y;
}

by danaqi_ @ 2023-08-22 09:34:08

@szlh_yanlikun
谢谢,我刚刚检查出来了,不过还是很谢谢您


by yanlikun @ 2023-08-22 09:35:07

不客气


by 1q2zb @ 2023-08-22 09:38:11

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int n,s,a,b,sum;
struct add{
    int x,y;
}ans[5005];

bool cmp(add a,add b){return a.y<b.y;}

int main(){
    scanf("%d%d%d%d",&n,&s,&a,&b);
    for(int i=1;i<=n;++i)
        scanf("%d%d",&ans[i].x,&ans[i].y);
    /*for(int i=1;i<=n;++i){
        for(int j=1;j<=n-i;++j){
            if(ans[j].y>ans[j+1].y)
                swap(ans[j],ans[j+1]);
        }
    }*/
    sort(ans+1,ans+1+n,cmp);

    int i=1;
    while(s-ans[i].y>=0&&i<=n){
        if(b+a>=ans[i].x){
            ++sum;
            s-=ans[i].y;
        }//既然不摘为什么减少体力(
        ++i;
    }
    printf("%d",sum);
    return 0;
}

有时候,两个标点就占40分

有时候,bug找一小时都找不回来十分(


by danaqi_ @ 2023-08-22 15:47:22

@1q2zb
cmp用的比较少不太会写,这里就没用


by the_time_end @ 2023-08-22 16:41:53

试试这种:

#include<bits/stdc++.h>
using namespace std;
struct apple {
    int high;
    int liqi;
} aa[5001];
bool cmp(apple a,apple b) {
    return a.liqi<b.liqi;
}
int main() {
    int n,s,a,b,ans=0;
    cin>>n>>s>>a>>b;
    for(int i=0; i<n; i++) {
        scanf("%d%d",&aa[i].high,&aa[i].liqi);
    }
    sort(aa,aa+n,cmp);
    for(int i=0; i<n; i++) {
        if(s<=0) break;
        if(aa[i].high<=(a+b) && aa[i].liqi<=s) {
            ans++;
            s-=aa[i].liqi;
        }
    }
    cout<<ans<<endl;
    return 0;
}

|