20分紧急!!!

P1478 陶陶摘苹果(升级版)

Pollococido @ 2023-06-18 13:41:30

#include <iostream>
#include <algorithm>
using namespace std;

struct node {
    int h, s;
}a[5010];

bool cmp(node x,node y){
    return x.s < y.s;
}

int main() {
    int n, st;
    int benchHeight, armHeight;
    cin >> n >> st;
    cout<<n<<' '<<st<<endl;
    cin >> benchHeight >> armHeight;
    int sum = benchHeight + armHeight;
    for (int i = 0; i < n; i++) {
        cin >> a[i].h >> a[i].s;
    }
    sort(a, a + n, cmp);
    int cnt = 0;
    for (int i = 0; i < n && st > 0; i++) {  // 修改为for循环,加入遍历完整个数组的判断
        if (st <= a[i].s) {
            if (sum < a[i].h){
                continue;
            }
            else{
                if (a[i].s != 0){
                    st -= a[i].s;
                }
                cnt++;
            }
        }
    }
    cout << cnt << endl;
    return 0;
}

by ltzx2022_kanxinyi_5 @ 2023-07-17 15:33:46

#include<bits/stdc++.h>
using namespace std;
int n,s,a,b,x,y,l,cnt,sm,sum;
double ar[5001];
int main(){
    cin>>n>>s>>a>>b;
    for(int i=0;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        if(x<=a+b)ar[cnt++]=y;
    }
    sort(ar,ar+cnt);
    while(s>=0&&sum<=cnt)
    {
        s-=ar[sum++];
    }
    cout<<sum-1;
    return 0;
} 

|