陶陶摘苹果(升级版)c++(基础分开算)


The topic is as follows
It's autumn again, and Tao Tao's apple tree bears n fruits. Tao Tao ran to pick apples again, this time he had a chair measuring a centimeter. When he can't reach, he will stand on the chair and try again.

This time, the difference from the first question of the popularization group of NOIp2005 is: Tao Tao moved the stool before, and only s strength is left. Of course, every time you pick an apple, you have to use a certain amount of strength. Tao Tao wants to know how many apples can be picked at most before s<0.

It is now known that n apples reach the height of the ground xi​, the height of the chair a, the maximum length of the pottery hand stretched b, the remaining strength of the pottery s, the strength required for the pottery to pick an apple, yi​, the pottery can be picked at most How many apples.

Input format

Line 1: Two numbers, number of apples, n, strength s.

Line 2: Two numbers The height of the chair a, the maximum length of the Taotao hand stretched b.

Row 3~row 3+n−1: Two numbers per row The height of the apple xi​, the strength required to pick this apple yi​.

There is only one integer, which represents the maximum number of apples that Tao Tao can pick.

Enter

8 15
20 130
120 3
150 2
110 7
180 1
50 8
200 0
140 3
120 2

Output

4

Problem analysis This is a simulated printing problem. We first rank the least effortlessly first, and then we can use the height to judge, so that we are always picking the least effortlessly, so that we can get the most apples.

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+6;
struct node{
	int x,y;
}s[maxn];
bool cmp(node q,node w)
{
	return q.y<w.y;
}
int main()
{
	int n,z,a,b;
	int ans=0;
	cin>>n>>z>>a>>b;
	for(int i=1;i<=n;i++)
	{
		cin>>s[i].x;
		cin>>s[i].y;
	}
	sort(s+1,s+1+n,cmp);
	for(int i=1;i<=n&&z>=0;i++)
	{
		if(s[i].x<=a+b&&z-s[i].y>=0)
		{
			z-=s[i].y;
			ans++;
		}	
	}
	cout<<ans<<endl;
}

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法编程张老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值