CF1474D.Cleaning

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

During cleaning the coast, Alice found nn piles of stones. The ii-th pile has aia_i stones.

Piles ii and i+1i + 1 are neighbouring for all 1≤i≤n−11 \leq i \leq n - 1. If pile ii becomes empty, piles i−1i - 1 and i+1i + 1 doesn't become neighbouring.

Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:

  • Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.

Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:

  • Before the start of cleaning, you can select two neighboring piles and swap them.

Determine, if it is possible to remove all stones using the superability not more than once.

在清理海岸时,爱丽丝发现了 nn 堆石子。第 ii 堆有 aia_i 颗石子。

对所有 1≤i≤n−11 \leq i \leq n - 1,第 ii 堆与第 i+1i + 1 堆是相邻的。若第 ii 堆变为空,则第 i−1i - 1 堆与第 i+1i + 1 堆不会因此变为相邻。

爱丽丝懒得亲自移除这些石子,于是请你代劳。她只允许你执行如下操作:

  • 选择两堆相邻的石子堆,若这两堆均非空,则从每堆中各移除一颗石子。

爱丽丝明白,有时仅靠上述操作无法将所有石子全部移除;因此她允许你使用如下“超级能力”:

  • 在开始清理前,你可以选择两堆相邻的石子堆并将它们交换位置。

请判断:是否能至多使用一次该超级能力,使得最终可以将所有石子全部移除。

输入格式

The first line contains a single integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases.

The first line of each test case contains the single integer nn (2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5) — the number of piles.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \leq a_i \leq 10^9) — the number of stones in each pile.

It is guaranteed that the total sum of nn over all test cases doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5)—— 石堆的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \leq a_i \leq 10^9)—— 每堆石头的数量。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.

对于每个测试用例,输出 YES 或 NO —— 表示是否能至多使用一次超级能力移除所有石子。

输入输出样例

  • 输入#1

    5
    3
    1 2 1
    3
    1 1 2
    5
    2 2 2 1 3
    5
    2100 1900 1600 3000 1600
    2
    2443 2445

    输出#1

    YES
    YES
    YES
    YES
    NO

说明/提示

In the first test case, you can remove all stones without using a superability: [1,2,1]→[1,1,0]→[0,0,0][1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0].

In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.

In the third test case, you can apply superability to the fourth and the fifth piles, thus getting a=[2,2,2,3,1]a = [2, 2, 2, 3, 1].

In the fourth test case, you can apply superability to the first and the second piles, thus getting a=[1900,2100,1600,3000,1600]a = [1900, 2100, 1600, 3000, 1600].

在第一个测试用例中,你可以不使用超级能力移除所有石子:[1,2,1]→[1,1,0]→[0,0,0][1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]。

在第二个测试用例中,你可以对第二堆和第三堆使用超级能力,然后按第一个测试用例的方式操作。

在第三个测试用例中,你可以对第四堆和第五堆使用超级能力,从而得到 a=[2,2,2,3,1]a = [2, 2, 2, 3, 1]。

在第四个测试用例中,你可以对第一堆和第二堆使用超级能力,从而得到 a=[1900,2100,1600,3000,1600]a = [1900, 2100, 1600, 3000, 1600]。

输入解题思路,AI测评打分。不知道怎么写?

首页