Header Ads

Header ADS

A. Shifting Stacks Codeforces Problem Solution in C++

A. Shifting Stacks

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (1t104) — the number of test cases.

The first line of each test case contains a single integer n (1n100). The second line of each test case contains n integers hi (0hi109) — 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, YESYesyesyEs will all be recognized as positive answer).

Example
input
Copy
6
2
1 2
2
1 0
3
4 4 4
2
0 0
3
0 1 0
4
1000000000 1000000000 1000000000 1000000000
output
Copy
YES
YES
YES
NO
NO
YES
Note

In the first test case there is no need to make any moves, the sequence of heights is already increasing.

In the second test case we need to move one block from the first stack to the second. Then the heights become 0 1.

In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights 3 4 5.

In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.

In the fifth test case we can only make one move (from the second to the third stack), which would make the heights 0 0 1. Both 0 1 0 and 0 0 1 are not increasing sequences, so the answer is NO. 



The solution:

#include <bits/stdc++.h>
#include <vector>
using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long int
#define ld long double
#define pp pop_back
#define ii pair<int , int>
double pi = 2 * acos(0.0);

template < typename T > ostream & operator<<(ostream & os, const vector < T > &v)
{
os << '{';
  for (const auto & x:v)
os << " " << x;
return os << '}';
}

void solve()
{
    ll n;
    cin >> n;
    ll vect[n];
    int in;
    for(int i=0; i<n; i++){
        cin >> vect[i];
    }
    ll c;
    for(int i=0; i<n-1; i++){
        c = vect[i]-i;
        if(c>0){
            vect[i+1] += c;
            vect[i] -= c;
        }
    }
    bool ans = false;
    for(int i=0; i<n-1; i++){
        if(vect[i]>=vect[i+1]){
          ans = true;
          break;  
        }
    }
    if(ans){
        cout << "NO" << endl;
    }
    else{
        cout << "YES" << endl;
    }
}   


int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);

int test_cases;
cin >> test_cases;
while (test_cases--)
{
solve();
}

return 0;
}

No comments

Powered by Blogger.