陶陶摘苹果(升级版)
题目描述
又是一年秋季时,陶陶家的苹果树结了 n n n 个果子。陶陶又跑去摘苹果,这次他有一个 a a a 公分的椅子。当他手够不着时,他会站到椅子上再试试。
这次与 NOIp2005 普及组第一题不同的是:陶陶之前搬凳子,力气只剩下 s s s 了。当然,每次摘苹果时都要用一定的力气。陶陶想知道在 s < 0 s<0 s<0 之前最多能摘到多少个苹果。
现在已知 n n n 个苹果到达地上的高度 x i x_i xi,椅子的高度 a a a,陶陶手伸直的最大长度 b b b,陶陶所剩的力气 s s s,陶陶摘一个苹果需要的力气 y i y_i yi,求陶陶最多能摘到多少个苹果。
输入格式
第 1 1 1 行:两个数 苹果数 n n n,力气 s s s。
第 2 2 2 行:两个数 椅子的高度 a a a,陶陶手伸直的最大长度 b b b。
第 3 3 3 行~第 3 + n − 1 3+n-1 3+n−1 行:每行两个数 苹果高度 x i x_i xi,摘这个苹果需要的力气 y i y_i yi。
输出格式
只有一个整数,表示陶陶最多能摘到的苹果数。
样例 #1
样例输入 #1
8 15
20 130
120 3
150 2
110 7
180 1
50 8
200 0
140 3
120 2
样例输出 #1
4
提示
对于 100 % 100\% 100% 的数据, n ≤ 5000 n\leq 5000 n≤5000, a ≤ 50 a\leq 50 a≤50, b ≤ 200 b\leq 200 b≤200, s ≤ 1000 s\leq 1000 s≤1000, x i ≤ 280 x_i\leq 280 xi≤280, y i ≤ 100 y_i\leq 100 yi≤100。
解析
这是一道典型的贪心问题,但是又不是很难,用简单的排序就可以了。
题目中涉及了力量这个因素,但是用1点力量和用5点力量摘掉的苹果又是一样的,所以可以对力量进行排序。
每个苹果都有两个数据:高度,摘掉所需力量。所以可以定义一个结构体:
struct apple{//定义结构体APPLE
int high;
int get;
}ale[5005];
这样就可以进行数据存储了,接下来就是sort排序,如果不知道什么事sort排序,可以看看我的博文
sort
AC CODE
#include <bits/stdc++.h>
using namespace std;
struct apple{//定义结构体APPLE
int high;
int get;
}ale[5005];
bool cmp(apple a,apple b){
return a.get<b.get;
}
int n,s,a,b;
int main()
{
//freopen("C:\\Users\\Sunsh\\Downloads\\P1478_3.in","r",stdin);
cin>>n>>s>>a>>b;
for(int i=1;i<=n;i++){
cin>>ale[i].high>>ale[i].get;//输入数据
}
sort(ale+1,ale+1+n,cmp);//从小到大排序
int ans=0,now=1;
while(s&& now<=n){
if(ale[now].high<=b+a && ale[now].get<=s){
ans++;
//te
s-=ale[now].get;
}
now++;//下一个苹果
}
// for(int i=1;i<=n;i++){
// cout<<ale[i].high<<ale[i].get<<endl;//输chu数据
// }
cout<<ans;
return 0;
}
//https://www.luogu.com.cn/problem/P1478
文章结束了,有帮助的请给个三连,谢谢