Codeforces Round #578 problem B :Block Adventure(暴力模拟+贪心)

题目描述:

传送门:http://codeforces.com/contest/1200/problem/B

Gildong is playing a video game called Block Adventure. In Block Adventure, there are n columns of blocks in a row, and the columns are numbered from 1 to n. All blocks have equal heights. The height of the i-th column is represented as hi, which is the number of blocks stacked in the i-th column.

Gildong plays the game as a character that can stand only on the top of the columns. At the beginning, the character is standing on the top of the 1-st column. The goal of the game is to move the character to the top of the n-th column.

The character also has a bag that can hold infinitely many blocks. When the character is on the top of the i-th column, Gildong can take one of the following three actions as many times as he wants:

if there is at least one block on the column, remove one block from the top of the i-th column and put it in the bag;
if there is at least one block in the bag, take one block out of the bag and place it on the top of the i-th column;
if i<n and |hi−hi+1|≤k, move the character to the top of the i+1-st column. k is a non-negative integer given at the beginning of the game. Note that it is only possible to move to the next column.
In actions of the first two types the character remains in the i-th column, and the value hi changes.

The character initially has m blocks in the bag. Gildong wants to know if it is possible to win the game. Help Gildong find the answer to his question.

Input
Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤1000). Description of the test cases follows.

The first line of each test case contains three integers n, m, and k (1≤n≤100, 0≤m≤106, 0≤k≤106) — the number of columns in the game, the number of blocks in the character’s bag at the beginning, and the non-negative integer k described in the statement.

The second line of each test case contains n integers. The i-th integer is hi (0≤hi≤106), the initial height of the i-th column.

Output
For each test case, print “YES” if it is possible to win the game. Otherwise, print “NO”.

You can print each letter in any case (upper or lower).

Example

input
5
3 0 1
4 3 5
3 1 2
1 4 7
4 10 0
10 20 10 20
2 5 5
0 11
1 9 9
99

output
YES
NO
YES
NO
YES

Note

In the first case, Gildong can take one block from the 1-st column, move to the 2-nd column, put the block on the 2-nd column, then move to the 3-rd column.

In the second case, Gildong has to put the block in his bag on the 1-st column to get to the 2-nd column. But it is impossible to get to the 3-rd column because |h2−h3|=3>k and there is no way to decrease the gap.

In the fifth case, the character is already on the n-th column from the start so the game is won instantly.

题目大意:

有n根柱子,每一根上面都有几个方块,Gildong从第一根柱子的顶部出发,如果两根柱子之间的方块数之差的绝对值小于等于k,Gildong可以从这根柱子跳到下一根柱子顶部。如果Gildong能够跳到最后一根柱子顶部,则输出YES,否则输出NO。Gildong还有一个无限大的背包,能够装任意个数的方块。当Gildong站在某根柱子上时,他能选择把这跟柱子上的几个方块放入背包以此来降低柱子的高度,也可以选择把自己背包中有的方块拿出来几个以此来提升柱子的高度。本题有多组测试数据。

解题思路:

当Gildong站在一根柱子上时,先把这根柱子上所有的方块都放入背包中,然后再往外拿,直到比下一根柱子上少k个,即刚好能跳到下一根柱子上。等到他跳到下一根柱子上时重复此操作,观察是否能跳到最后一根柱子上。也就是一个贪心的思想。

注意事项:

本题有一个很重要的注意点就是a[i]=max(a[i+1]-k,0),因为a[i+1]-k可能时小于0的,这样的话对后面m的运算就会产生影响,等于说给背包中无中生有了几个方块。(因为本身需要往外放0个就行了,你往外放了负数个,相当于又给背包拿进去了几个,但这几个实际上并不存在)

同样重要的一点:数组和flag别忘了初始化

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath> 
using namespace std;
int main()
{
	int t=0,n=0,m=0,k=0,i=0,flag=1;
	int a[200]={0};
	cin>>t;
	while(t--)
	{
		cin>>n>>m>>k;
		memset(a,0,sizeof(a));//初始化 
		flag=1;//flag的初始化,同样很容易忘 
		for(i=0;i<n;i++)
		cin>>a[i];
		
		if(n==1)
		{
			cout<<"YES"<<endl;
			continue;
		}
		
		if(n!=1)
		{
			for(i=0;i<n;i++)
			{
				m=m+a[i];//把这根柱子上所有的先装进包里 
				a[i]=max(a[i+1]-k,0);
				/*这根柱子上最少有多少个才能满足要求 
				注意 a[i+1]-k可能小于0!!,如果按照这个值进行计算的话,后面m的值还会增大
				一定要注意这个特殊情况!*/ 
				m=m-a[i];//从包里拿出来这么多块 
				if(m>=0)//包里还有或者包里是空的 
				continue;//下一根柱子 
				if(m<0)//包里还欠着 
				{
					cout<<"NO"<<endl;
					flag=0;
					break;
				}
			}
		}
		if(flag==1)
		cout<<"YES"<<endl;
	}
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值