Codeforces Round #578 (Div. 2):1200B. Block Adventure
题目:
Gildong is playing a video game called Block Adventure. In Block Adventure, there are 𝑛 columns of blocks in a row, and the columns are numbered from 1 to 𝑛. All blocks have equal heights. The height of the 𝑖-th column is represented as ℎ𝑖, which is the number of blocks stacked in the 𝑖 -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 𝑛 -th column.
The character also has a bag that can hold infinitely many blocks. When the character is on the top of the 𝑖 -th column, Gildong can take one of the following three actions as many times as he wants:
1.if there is at least one block on the column, remove one block from the top of the 𝑖 -th column and put it in the bag;
2.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 𝑖
-th column;
3.if 𝑖<𝑛 and |ℎ𝑖−ℎ𝑖+1|≤𝑘, move the character to the top of the 𝑖+1-st column. 𝑘 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 𝑖
-th column, and the value ℎ𝑖 changes.
The character initially has 𝑚 blocks in the bag. Gildong wants to know if it is possible to win the game. Help Gildong find the answer to his question.
输入:
Each test contains one or more test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤1000). Description of the test cases follows.
The first line of each test case contains three integers 𝑛
, 𝑚, and 𝑘 (1≤𝑛≤100, 0≤𝑚≤106, 0≤𝑘≤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 𝑘 described in the statement.
The second line of each test case contains 𝑛 integers. The 𝑖-th integer is ℎ𝑖 (0≤ℎ𝑖≤106), the initial height of the 𝑖-th column.
输出:
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).
样例:
输入:
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
输出:
YES
NO
YES
NO
YES
分析:
题意很好理解,思路也很清晰,能否转移到下一h上的条件就是转移过去后bag里的blocks的最大值 >= 0,在每一次操作贪心都取最好的决策(即bag+最多的,-最少的),但是要注意讨论h[i+1] < k
的情况。
代码:
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
int t;
long long h[100001] = {0};
long long bag = 0;
long long n,m,k;
bool judge(long long i)
{
if(h[i] >= h[i + 1] - k)
{
if(h[i + 1] < k)
{
bag += h[i];
}
else
{
bag += h[i] - (h[i + 1] - k);
}
return true;
}
else
{
bag -= h[i + 1] - k - h[i];
if(bag < 0)
{
return false;
}
return true;
}
}
int main()
{
cin>>t;
while(t--)
{
int temp = 0;
memset(h, 0, sizeof(h));
cin>>n>>m>>k;
bag = m;
for(long long i = 0;i < n;i++)
{
cin>>h[i];
}
for(long long i = 0;i < n - 1;i++)
{
if(!judge(i))
{
cout<<"NO"<<endl;
temp = 1;
break;
}
}
if(!temp)
{
cout<<"YES"<<endl;
}
}
return 0;
}