P1478 陶陶摘苹果(升级版)(C++_贪心)

题目描述

又是一年秋季时,陶陶家的苹果树结了 n 个果子。陶陶又跑去摘苹果,这次他有一个 a 公分的椅子。当他手够不着时,他会站到椅子上再试试。

这次与 NOIp2005 普及组第一题不同的是:陶陶之前搬凳子,力气只剩下 s 了。当然,每次摘苹果时都要用一定的力气。陶陶想知道在 s<0s<0 之前最多能摘到多少个苹果。

现在已知 n 个苹果到达地上的高度 x i x_i ,椅子的高度 a,陶陶手伸直的最大长度 b,陶陶所剩的力气 s,陶陶摘一个苹果需要的力气 y i y_i ,求陶陶最多能摘到多少个苹果。

输入格式

第 1 行:两个数 苹果数 n,力气 s。

第 2 行:两个数 椅子的高度 a,陶陶手伸直的最大长度 b。

第 3 行~第 3+n−1 行:每行两个数 苹果高度 x i x_i ,摘这个苹果需要的力气 y i y_i

输出格式

只有一个整数,表示陶陶最多能摘到的苹果数。

输入输出样例

输入 #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\% 的数据, n 5000 n\leq 5000 , a 50 a\leq 50 , b 200 b\leq 200 , s 1000 s\leq 1000 , x i 280 x_i\leq 280 , y i 100 y_i\leq 100

思路

偶尔切切水题真的是浑身舒畅~

源码

#include<bits/stdc++.h>
using namespace std;
int a[5010];
bool cmp(int a, int b)
{
	return a < b;
}
int main()
{
	int n, s, h, h_, ans = 0;
	cin >> n >> s >> h >> h_;
	h += h_;
	int num = 0;
	h_ = n;
	while(h_--)
	{
		int temp1, temp2;
		cin >> temp1 >> temp2;
		if (temp1 <= h)
			a[++num] = temp2;
	}
	sort(a + 1, a + 1 + num, cmp);
	for (int i = 1; i <= num; i++)
		if (s >= a[i])
		{
			s -= a[i];
			ans++;
		}
	cout<< ans;
	return 0;
}
发布了228 篇原创文章 · 获赞 30 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43510916/article/details/104238972