Codeforces Round #703 (Div. 2)A. Shifting Stacks##
问题描述:
A. Shifting Stacks
time limit per test1 second
memory limit per test256 megabytes
You have n stacks of blocks. The i-th stack contains hi blocks and it’s height is the number of blocks in it. In one move you can take a block from the i-th stack (if there is at least one block) and put it to the i+1-th stack. Can you make the sequence of heights strictly increasing?
Note that the number of stacks always remains n: stacks don’t disappear when they have 0 blocks.
Input
First line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤100). The second line of each test case contains n integers hi (0≤hi≤109) — starting heights of the stacks.
It’s guaranteed that the sum of all n does not exceed 104.
Output
For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
inputCopy
6
2
1 2
2
1 0
3
4 4 4
2
0 0
3
0 1 0
4
1000000000 1000000000 1000000000 1000000000
outputCopy
YES
YES
YES
NO
NO
YES
思路分析:
我写的思路非常简单,就是特例化。将所有的数列全部转化为等差数列的形式,从0~n,每个数都进行判断并进行相应的差值计算,若出现不能满足的情况,则直接输出“NO”。
解决方案:
提示:这里填写该问题的具体解决方案:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=10010;
int t,n;
LL h[N];
int main()
{
cin>>t;
while(t--)
{
memset(h,0,N);
int temp=0;
cin>>n;
for(int i=0;i<n;i++)
cin>>h[i];
for(int i=0;i<n;i++)
{
if(h[i]>=i)
{
h[i+1]+=h[i]-i;
h[i]=i;
}
else
{
cout<<"NO"<<endl;
temp=1;
break;
}
}
if(temp==0) cout<<"YES"<<endl;
}
return 0;
}