Codeforces 1092D1 Great Vova Wall (Version 1)

发布时间 2023-07-09 00:36:19作者: lhzawa

发现不管 \(a_i\) 多大都可以一直 \(a_i\leftarrow a_i + 2\) 使所有 \(a_i\) 的取值变为两个相邻的数,那就只需要考虑奇偶的限制了。

发现若 \(2\) 个奇偶相同的 \(a_i, a_{i + 1}\),那这 \(2\) 个数的奇偶性就可以变化。
所以可以维护一个栈,若栈顶 \(2\) 个数的奇偶性相同直接删去即可。
若最后栈中元素数量 \(\le 1\) 则有解(\(= 1\) 时考虑把其他可以变化奇偶性的数都变为剩下这个数的奇偶性),否则无解。

时间复杂度 \(O(n)\)

// lhzawa(https://www.cnblogs.com/lhzawa/)
// Problem: D1. Great Vova Wall (Version 1)
// Contest: Codeforces - Codeforces Round 527 (Div. 3)
// URL: https://codeforces.com/problemset/problem/1092/D1
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N];
int stk[N], top;
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		a[i] &= 1;
	}
	for (int i = 1; i <= n; i++) {
		stk[++top] = a[i];
		if (top >= 2 && stk[top] == stk[top - 1]) {
			top -= 2;
		}
	}
	printf("%s", top <= 1 ? "YES" : "NO");
	return 0;
}