Codeforces-1474 D. Cleaning(前缀和)

2024-04-15 23:08

本文主要是介绍Codeforces-1474 D. Cleaning(前缀和),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

During cleaning the coast, Alice found 𝑛 piles of stones. The 𝑖-th pile has 𝑎𝑖 stones.

Piles 𝑖 and 𝑖+1 are neighbouring for all 1≤𝑖≤𝑛−1. If pile 𝑖 becomes empty, piles 𝑖−1 and 𝑖+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.

Input
The first line contains a single integer 𝑡 (1≤𝑡≤104) — the number of test cases.

The first line of each test case contains the single integer 𝑛 (2≤𝑛≤2⋅105) — the number of piles.

The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109) — the number of stones in each pile.

It is guaranteed that the total sum of 𝑛 over all test cases doesn’t exceed 2⋅105.

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

Example
inputCopy
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
outputCopy
YES
YES
YES
YES
NO
Note
In the first test case, you can remove all stones without using a superability: [1,2,1]→[1,1,0]→[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 𝑎=[2,2,2,3,1].

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

题意:
你可以任选相邻两个非0数,使得两者都减1。
你可以选择相邻两个数交换位置,至多操作1次。
求能否使得每个数都变成0。

思路:

  • 从边界开始考虑,很明显边界的数只能被边上一个数抵消掉(因为边上只有一个数)。因此可以维护一个前缀和代表从第一个数依次抵消到了第 i i i个数后,第 i i i个数剩下的数(如果不能抵消到第 i i i个数,结果就是INF)。同理可以维护个后缀和代表从最后的数依次抵消到第 i i i个数后第 i i i个数的值。
  • 假设没有交换操作,那么就是满足 p r e [ i ] = s u f [ i + 1 ] pre[i]=suf[i+1] pre[i]=suf[i+1]即可,也就是一个前缀和等于后缀和。
  • 如果有交换操作,那也好整,就只前缀和和后缀和中间的两个数分别考虑一下,此时变成了4个数 p r e [ i ] , a [ i + 2 ] , a [ i + 1 ] , s u f [ i + 3 ] pre[i],a[i+2],a[i+1],suf[i+3] pre[i],a[i+2],a[i+1],suf[i+3],只要保证 a [ i + 2 ] ≥ p r e [ i ] , a [ i + 1 ] ≥ s u f [ i + 3 ] a[i+2]≥pre[i],a[i+1]≥suf[i+3] a[i+2]pre[i],a[i+1]suf[i+3],且 a [ i + 2 ] − p r e [ i ] = a [ i + 1 ] − s u f [ i + 1 ] a[i+2]-pre[i]=a[i+1]-suf[i+1] a[i+2]pre[i]=a[i+1]suf[i+1],就说明交换 a [ i + 1 ] , a [ i + 2 ] a[i+1],a[i+2] a[i+1],a[i+2]后可以全部消掉。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>using namespace std;typedef long long ll;const int INF = 0x3f3f3f3f;
const int maxn = 2e5 + 7;
int pre[maxn],suf[maxn],a[maxn];
int main() {int T;scanf("%d",&T);while(T--) {int n;scanf("%d",&n);for(int i = 1;i <= n;++i) scanf("%d",&a[i]);suf[n + 1] = 0;for(int i = 1;i <= n;i++) {if(a[i] >= pre[i - 1]) {pre[i] = a[i] - pre[i - 1];} else {pre[i] = INF;}}for(int i = n;i >= 1;i--) {if(a[i] >= suf[i + 1]) {suf[i] = a[i] - suf[i + 1];} else {suf[i] = INF + 1e6;}}int flag = 0;for(int i = 1;i < n;i++) {if(pre[i] == suf[i + 1]) {flag = 1;break;} else { //i和i+1交换int x = a[i + 1],y = a[i];if(x >= pre[i - 1] && y >= suf[i + 2]) {x -= pre[i - 1];y -= suf[i + 2];if(x == y) {flag = 1;break;}}}}if(flag) {printf("YES\n");} else {printf("NO\n");}}return 0;
}

这篇关于Codeforces-1474 D. Cleaning(前缀和)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907183

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

【LeetCode热题100】前缀和

这篇博客共记录了8道前缀和算法相关的题目,分别是:【模版】前缀和、【模版】二维前缀和、寻找数组的中心下标、除自身以外数组的乘积、和为K的子数组、和可被K整除的子数组、连续数组、矩阵区域和。 #include <iostream>#include <vector>using namespace std;int main() {//1. 读取数据int n = 0, q = 0;ci

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

前缀和 — 利用前缀信息解决子数组问题

【前缀和的核心思想是预先处理数组来快速计算任意子数组的和,基本上用于数组和序列问题。】 前缀和算法具体步骤 构造前缀和数组: 给定一个数组nums,其前缀和数组prex定义为prex[i]表示为数组nums从起始位置到第i个位置的元素累加和。构建前缀和公式: p r e x [ i ] = n u m s [ i ] ( i = = 0 ) p r e x [ i ] = p r e x

Codeforces#295(Div.2)A、B(模拟+BFS)

解题报告链接:点击打开链接 C. 题目链接:点击打开链接 解题思路: 对于给定的字符串,取出现次数最多的字母(可以同时有多个)。由这些字母组成长度为n的字符串,求有多少种组合。最后用数学知识即可。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <climits>

Codeforces Round #281 (Div. 2)A(构造+暴力模拟)

题目链接:http://codeforces.com/problemset/problem/493/A 解题思路: 暴力的判断,分三种情况去判断即可。注意如果之前已经被罚下场后,那么在后面的罚下情况不应该算在输出结果内。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <co