dalao,帮忙看看2RE

P1478 陶陶摘苹果(升级版)

XSean @ 2022-09-03 11:08:16


#include<bits/stdc++.h>
using namespace std;
int n,s;
struct apple{
    int h,lq;
}arr[5050];
bool cmp1(apple x,apple y){
    return x.h<y.h;
}
bool cmp2(apple x,apple y){
    return x.lq<y.lq;
}
int main(){
//  freopen("P1478.in","r",stdin);
//  freopen("P1478.out","w",stdout);
int a,b;
cin>>n>>s;
cin>>a>>b;
int h1=a+b;
for(int i=1;i<=n;i++){
    cin>>arr[i].h>>arr[i].lq;
}
sort(arr+1,arr+1+n,cmp1);
int k=0,k1=0;
while(arr[k+1].h<=h1){
    k++;
}
sort(arr+1,arr+1+k,cmp2);
while(s>=arr[k1+1].lq&&k1+1<=k){
    k1++;
    s-=arr[k1].lq;
}
cout<<k1;
//  fclose(stdin);
//  fclose(stdout);
    return 0;
}

by bamboo12345 @ 2022-09-03 11:14:52

@Sean_xzx 加一个k<=n?


by 已注销!^sZA2#G @ 2022-09-03 11:15:36

#include<iostream>
#include<algorithm> 
using namespace std;
int n,s,a,b,x_,y_,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>>x_>>y_;
        if(x_<=a+b){
            can++;
            ap[can].xi=x_;
            ap[can].yi=y_;
        }
    }
    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;
} 

@Sean_xzx


by 已注销!^sZA2#G @ 2022-09-03 11:16:04

@Sean_xzx 直接贴代码


by XSean @ 2022-09-03 11:17:25

@bamboo123 在哪里


by bamboo12345 @ 2022-09-03 11:21:42

@Sean_xzx 第一个while那里


by Time_Limit_Exceed @ 2022-09-03 11:58:33

@Sean_xzx 加&&k<n在while里


|