Codeforces Round #703 (Div. 2)A. Shifting Stacks

A. Shifting Stacks

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

题目描述

You have stacks of blocks. The -th stack contains blocks and it’s height is the number of blocks in it. In one move you can take a block from the -th stack (if there is at least one block) and put it to the -th stack. Can you make the sequence of heights strictly increasing?

Note that the number of stacks always remains : stacks don’t disappear when they have blocks.

翻译:
你有n块堆栈。第 i 堆栈包含 Hi 块和它的高度就是它的块数。在一个移动中,你可以采取一个街区从第 i 堆栈(如果至少有一个块),并把它放在第 i+1 堆栈。你能使高度序列严格增加吗?
请注意,堆栈的数量始终保持不变n:堆栈不会消失时,他们有0块。

Input

First line contains a single integer — the number of test cases.

The first line of each test case contains a single integer . The second line of each test case contains integers — starting heights of the stacks.

It’s guaranteed that the sum of all does not exceed .

翻译:
第一行包含单个整数t (1≤t≤104)—测试案例的数量。

每个测试案例的第一行包含一个整数n (1≤n≤100).每个测试案例的第二行包含n整数HⅠ (0≤HⅠ≤109)—堆栈的起始高度。

它保证,所有的总和n不超过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).

翻译:
对于每个测试案例输出是,如果你能使高度序列严格增加,没有否则。

在任何情况下,您都可以打印每个字母(例如,YES, Yes, yes, yEs 都会被确认为正面答案)。

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

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 .

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 .

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 . Both and are not increasing sequences, so the answer is NO.
翻译:
在第一个测试案例中,无需进行任何移动,高度序列已经在增加。

在第二个测试案例中,我们需要将一个块从第一个堆栈移动到第二个方块。然后高度变得0 1.

在第三个测试案例中,我们可以将一个块从第一个堆栈移动到第二个堆栈,然后从第二个堆栈移动到第三个方块,这将使高度3 4 5.

在第四个测试案例中,我们不能移动,但序列没有增加,所以答案是否定的。

在第五个测试案例中,我们只能移动一个(从第二个到第三个堆栈),这将使高度0 0 1.两者0 1 0和0 0 1没有增加序列,所以答案是否定的。

解题思路

从题意中我们了解到,在一个堆中往另一个堆中移动只能是从 i 到 i+1, 这就意味着移动堆中的砖块只能从前往后移,而且他需要递增方式进行堆砖,我们就可以采用最基本的方式进行判断此种排列方式能否满足
在这里插入图片描述

参考代码

#include <iostream>

using namespace std;

int main()
{
    int n,i;
    cin>>n;

    long long int t;//注意题目说明哪些变量需要长整型
    while(n--){
        cin>>t;
        long long int sum=0,should=0;
        long long int a[t];
        int flag=1;
        for(i=0;i<t;i++){
        	cin>>a[i];
        	sum+=a[i];
        	should+=i;//站在第 i 个堆栈中,统计0-i 堆中至少要含有的砖的数量,就如上图所示的最基本的砖头数量 
			if(sum<should)//当最基本都不能满足的话就说明无法完成,直接标记
				flag=0; 
		}
        	
        if(flag)
            cout<<"YES";
        else
            cout<<"NO";
        cout<<endl;
    }

    return 0;
}

没明白的话,欢迎来打扰;
学会了的话,留下一个赞,互相鼓励哦!!!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值