80tps,蒟蒻求调!!!

P1478 陶陶摘苹果(升级版)

rjp20120408 @ 2024-10-24 21:57:29

测试点信息 3ms/564.00KB AC

1

Accepted, 得分 20.ok accepted

4ms/676.00KB AC

2

Accepted, 得分 20.ok accepted

4ms/564.00KB AC

3

Accepted, 得分 20.ok accepted

0ms/0B RE

4

Runtime Error. Received signal 11: Segmentation fault with invalid memory reference.

5ms/556.00KB AC

5

Accepted, 得分 20.ok accepted

只有80分,第四个测试点RE。

#include<bits/stdc++.h>
using namespace std;
struct apple{
    int high;
    int strength;
    bool flag=false;
}l[5001];
bool cmp(apple x,apple y){
    return x.strength<y.strength;
}
int main(){
    int n,s,b,a,i,ans=0;
    cin>>n>>s>>a>>b;
    int most=a+b;
    for(i=1;i<=n;i++){
        cin>>l[i].high>>l[i].strength;
        if(l[i].high<=most)
        l[i].flag=true;
    }
    sort(l+1,l+n+1,cmp);
    i=1;
    while(s-l[i].strength>=0){
        if(l[i].flag==true){
            ans++;
        s-=l[i].strength;
        }
        i++;
    }
    cout<<ans;
    return 0;
}

by Specthraimn @ 2024-10-24 22:08:14

while(s-l[i].strength>=0)

加上

while(i<n&&s-l[i].strength>=0)

by Specthraimn @ 2024-10-24 22:08:44

不然会死循环


by Ff472130 @ 2024-10-24 22:10:17

#include<bits/stdc++.h>
using namespace std;
struct apple{
    int high;
    int strength;
    bool flag=false;
}l[5001];
bool cmp(apple x,apple y){
    return x.strength<y.strength;
}
int main(){
    int n,s,b,a,i,ans=0;
    cin>>n>>s>>a>>b;
    int most=a+b;
    for(i=1;i<=n;i++){
        cin>>l[i].high>>l[i].strength;
        if(l[i].high<=most)
        l[i].flag=true;
    }
    sort(l+1,l+n+1,cmp);
    i=1;
    while(s-l[i].strength>=0&&i<=n){
        if(l[i].flag==true){
            ans++;
        s-=l[i].strength;
        }
        i++;
    }
    cout<<ans;
    return 0;
}

by rjp20120408 @ 2024-11-03 19:11:42

@Specthraimn 谢谢,代码已AC,已关。


|