题目描述
又是一年秋季时,陶陶家的苹果树结了 �n 个果子。陶陶又跑去摘苹果,这次他有一个 �a 公分的椅子。当他手够不着时,他会站到椅子上再试试。
这次与 NOIp2005 普及组第一题不同的是:陶陶之前搬凳子,力气只剩下 �s 了。当然,每次摘苹果时都要用一定的力气。陶陶想知道在 �<0s<0 之前最多能摘到多少个苹果。
现在已知 �n 个苹果到达地上的高度 ��xi,椅子的高度 �a,陶陶手伸直的最大长度 �b,陶陶所剩的力气 �s,陶陶摘一个苹果需要的力气 ��yi,求陶陶最多能摘到多少个苹果。
输入格式
第 11 行:两个数 苹果数 �n,力气 �s。
第 22 行:两个数 椅子的高度 �a,陶陶手伸直的最大长度 �b。
第 33 行~第 3+�−13+n−1 行:每行两个数 苹果高度 ��xi,摘这个苹果需要的力气 ��yi。
输出格式
只有一个整数,表示陶陶最多能摘到的苹果数。
输入输出样例
输入 #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% 的数据,�≤5000n≤5000, �≤50a≤50, �≤200b≤200, �≤1000s≤1000, ��≤280xi≤280, ��≤100yi≤100。
贪心排序后直接模拟就能出答案。
ac代码
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<math.h>
#include<iomanip>
#include<set>
#include<queue>
#include<stack>
#include<map>
using namespace std;
int n, s, a, b,ans=0;//定义苹果数,力气,手长,凳子长
struct MyStruct
{
int x;
int y;
}qq[10000];
bool cmp(MyStruct x1, MyStruct x2)
{
return x1.y < x2.y;
}
int main()
{
cin >> n >> s >> a >> b;
for (int i = 0; i < n; i++)
{
cin >> qq[i].x >> qq[i].y;
}
sort(qq, qq + n, cmp);//按消耗力气最小排列
for (int i = 0; i < n; i++)
{
if (s > qq[i].y && a + b >= qq[i].x)//如果符合条件更新力气和答案
{
ans++;
s = s - qq[i].y;
}
}
cout << ans;
}