20求助!

P1478 陶陶摘苹果(升级版)

ggpw_XNW @ 2023-12-12 17:59:23

#include<iostream>
#include<algorithm>
using namespace std;
int n , s , a , b , x[5000] , y[5000] , ans;
bool cmp(int a , int b){
    int a1 , b1;
    for(int i=0;i<n;i++){
        if(x[i]==a){
            a1 = i;
        }
    }
    for(int i=0;i<n;i++){
        if(x[i]==b){
            b1 = i;
        }
    }
    if(a>b){
        swap(y[a1],y[b1]);
        return true;
    }
    return false;
}
int main(){
    cin >> n >> s >> a >> b;
    for(int i=0;i<n;i++){
        cin >> x[i] >> y[i];
    }
    sort(x,x+n,cmp);
    for(int i=0;i<n;i++){
        if(s<=0){
            break;
        }
        if(x[i]<=a+b&&s>0){
            s -= y[i];
            ans++;
        }
    }
    cout << ans;
    return 0;
}

by chenshixue @ 2023-12-16 14:50:45

就过了一个特判


by xuyuteng12345 @ 2024-04-21 10:22:20

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n,s,a,b,o,ans;
struct r{
    int x,y;
}l[5001],l1[5001];
bool cmp(r a,r b){
    return a.y<b.y;
}
int main(){
    cin>>n>>s>>a>>b;
    if(n==0){
        cout<<0;
        return 0;
    }
    a+=b;
    for(int i=0;i<n;i++){
        cin>>l[i].x>>l[i].y;
        if(l[i].x<=a)l1[o++]=l[i];
    }
    sort(l1,l1+o,cmp);
    for(int i=0;i<=o;i++){
        if(l1[i].y>s)break;
        ans++,s-=l1[i].y;
    }
    cout<<ans;
    return 0;
}

|