求助???

P1478 陶陶摘苹果(升级版)

WXY_yyds @ 2023-05-01 17:31:33

#include<bits/stdc++.h>
using namespace std;
struct m{
    int x,y;
}l[10001];

bool cmp(m c,m b){
    return c.y < b.y; 
}

int main(){
    int n,s,a,b,sum = 0;
    cin>>n>>s>>a>>b;
    for(int i = 1;i <= n;i++){
        cin>>l[i].x>>l[i].y;
    }
    sort(a+1,a+n+1,cmp);
    int i = 0;
    while(s >= 0){
        i++;
        s -= l[i].y;
        if(a + b > l[i].x) sum++;
    }
    cout<<sum;
    return 0;
}

by gitmeetcat @ 2023-09-02 19:03:11

#include "bits/stdc++.h"
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, s, a, b, minid, x[5001], y[5001],num=0;
    cin >> n >> s >> a >> b;
    for (int i = 1; i <= n ; ++i)
    {
        cin >> x[i] >> y[i];
    }
    for (int i = 1; i <= n; ++i)
    {
        minid = i;
        for (int j = i; j <= n; ++j)
        {
            if (y[j] < y[minid])
                minid = j;
        }
        swap(y[i], y[minid]);
        swap(x[i], x[minid]);
    }
    for (int i = 1; i <= n; i++)
    {
        if(s>=y[i]&&x[i]<=a+b)//cout << x[i] << ' ' << y[i] << endl;
        {
            s-=y[i];
            num++;
            if(s<=0)
            {
                break;
            }
        }
    }
    cout<<num;

    return 0;
}

|