SPOJ NITK06 16121 MODIFY SEQUENCE

Description

Suppose we have a sequence of non-negative integers, Namely a1, a2, ... ,an. At each time we can choose one term ai with 0 < i < n and we subtract 1 from both ai and ai+1. We wonder whether we can get a sequence of all zeros after several operations.

Input

The first line is the number of test cases T ( 0 < T <= 20).

The first line of each test case is a number N. (0 < N <= 10000) The next line is N non-negative integers, 0 <= ai <= 109

Output

If it can be modified into all zeros with several operations output “YES” in a single line, otherwise output “NO” instead.

Example

Input:
2
2
1 2
2
2 2

Output:
NO
YES

Explanation:

It is clear that [1 2] can be reduced to [0 1] but no further to convert all integers to 0. Hence, the output is NO.

In second case, output is YES as [2 2] can be reduced to [1 1] and then to [0 0] in just two steps.
题意:给出一个长为n的数列ai(1<=i<=n),每个元素都非负。每次你可以选择一个下标x(1<=x<=n-1),将ax和ax+1同时减1。问经过若干次操作,能否将原数列变成一个元素全部为0的序列。
最后如果所有元素都能变为0,则满足条件。
#include <iostream>
using namespace std;

int num[10000];

int main()
{
    int N,T;
    cin >> T;
    while(T--)
    {
        cin >> N;
        for(int i = 0;i < N;i++)
        {
            cin >> num[i];
        }

        for(int i = 0;i < N;i++)
        {
            for(;;)
            {
                if(num[i] != 0 && num[i+1] != 0)
                {
                    num[i]--;
                    num[i+1]--;
                }
                else if(num[i] == 0 || num[i+1] == 0)
                {
                    break;
                }
            }
        }
        int flag = 1;
        for(int i = 0;i < N;i++)
        {
            if(num[i] != 0)
            {
                flag = 0;
                break;
            }
        }
        if(flag) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}

贴的是比赛的代码,时间紧,任务重,写的比较丑。主要的思路就是模拟,数据给的很宽裕,直接暴力就可以解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值