陶陶摘苹果(升级版)
题目描述
又是一年秋季时,陶陶家的苹果树结了 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。
问题链接: P1478 陶陶摘苹果(升级版)
问题分析: 贪心问题,不解释。
参考链接: (略)
题记: (略)
AC的C++语言程序如下:
/* P1478 陶陶摘苹果(升级版) */
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5000;
struct Apple {
int x, y;
} ap[N];
int cmp(Apple a, Apple b)
{
return a.y < b.y;
}
int main()
{
int n, s, a, b;
cin >> n >> s >> a >> b;
int k = 0;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
if (x <= a + b) {
ap[k].x = x;
ap[k].y = y;
k++;
}
}
sort(ap, ap + k, cmp);
int cnt = 0;
for (int i = 0; s >= ap[i].y && i < k; i++)
cnt++, s -= ap[i].y;
cout << cnt;
return 0;
}