python3 60分 求大佬帮助

P1478 陶陶摘苹果(升级版)

Chen_Yu_Wei @ 2024-08-12 11:17:07

n,s = map(int,input().split())
a,b = map(int,input().split())
l = []
ans = 0

for i in range(n):
    x = list(map(int,input().split()))
    if x[0] <= a+b:
        l.append(x)

l.sort(key=lambda x:x[1])

i = 0
while 1:
    s -= l[i][1]
    if s>0:
        ans += 1
    else:
        break
    i += 1

print(ans)

by hkjkjk @ 2024-08-16 19:38:51

我不是python的,c++这样就过了

#include<iostream>
#include<algorithm> 
using namespace std;
int n,s,a,b,x1,y1,can,rest,ans;
struct apple{
    int xi,yi;
}ap[50005];
int cmp(apple x,apple y){
    return x.yi<y.yi;
}
int main(){
    cin>>n>>s>>a>>b;
    for(int i=1;i<=n;i++){
        cin>>x1>>y1;
        if(x1<=a+b){
            can++;
            ap[can].xi=x1;
            ap[can].yi=y1;
        }
    }
    sort(ap+1,ap+can+1,cmp);
    rest=s;
    ans=0;
    for(int i=1;rest>=ap[i].yi&&i<=can;i++){
        ans++;
        rest-=ap[i].yi;
    }
    cout<<ans;
    return 0;
} 

by Allen20 @ 2024-08-26 10:39:14

bool cmp吧


by Allen20 @ 2024-08-26 10:39:50

@hkjkjk


|