完全不会,大佬们帮帮蒟蒻吧

P1478 陶陶摘苹果(升级版)

Zhc13909022402 @ 2024-06-27 20:28:00

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,s;
    int a,b;
    int x,y;
    int cnt;
    cin>>n>>s;
    cin>>a>>b;
    int ab=a+b;
    for(int i=0;i<=n;i++)
    {
        cin>>x>>y;
        if(s>=0)
        {
            if(ab>=x)
            {
                s=s-y;
                cnt++;
            }
        }
        else continue;
    }
    cout<<cnt;
}

by NullPointerExpection @ 2024-06-27 20:35:58

你可以使用 std::sort 来给 y_i 排序


by canwen @ 2024-06-27 20:36:51

正解 贪心

#include<iostream>
#include<algorithm>
using namespace std;
struct q{
    int x,y;
}p[5001];
bool cmp(q a,q b){
    return a.y<b.y;
}
int main(){
    int n,s,a,b,ans=0;
    cin>>n>>s>>a>>b;
    a+=b;
    for(int i=1;i<=n;i++) cin>>p[i].x>>p[i].y;
    sort(p+1,p+1+n,cmp);
    for(int i=1;i<=n;i++){
        if(s<=0) break;
        if(s-p[i].y>=0&&a>=p[i].x){
            s-=p[i].y;
            ans++;
        } 
    }
    cout<<ans;
    return 0;
}

by hashcode1024 @ 2024-06-27 20:39:37

力气得排序,试试结构体排序呗


by liboya5074 @ 2024-06-27 20:47:28

@Zhc13909022402 不一定按照最优方式输入,要进行结构体排序。

话说你学多久了?


by Zhc13909022402 @ 2024-06-27 20:49:07

@liboya5074 我六年级,结构体学了


by liboya5074 @ 2024-06-27 20:49:51

那最好多看看题


by Zhc13909022402 @ 2024-06-27 20:50:41

@canwen2 我没学贪心


by Zhc13909022402 @ 2024-06-27 20:53:37

@liboya5074 我学到自定义函数了


by canwen @ 2024-06-27 22:52:52

@Zhc13909022402 加油加油我只是来摸鱼的


|