CF1291B 题解
Luogu-CF1291B
题目分析
对于一个序列 \(a\),我们需要找一个分界线,使左半边严格单调递增,而右半边严格单调递减。
我们可以构建出最小的满足题意的序列 \(b\),如下面这样:
\[0,1,2,...,M-1,M,M-1,...,2,1,0
\]
那么,我们可以将 \(a_i\) 与 \(b_i\) 作比较,如果 \(a_i < b_i\),那么一定不合法。
当 \(n\) 为奇数时,上方叙述成立。但是当 \(n\) 为偶数时,我们需要构造这样两个 \(b\) 数列:
\[0,1,2,...,M-1,M,M-1,M-2,...,2,1,0
\]
\[0,1,2,...,M-2,M-1,M,M-1,...,2,1,0
\]
然后比较是否 \(a_i < b_i\) 即可。
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll T, n;
ll a[300005], b[300005];
void C(){ //不能初始化整个数组,否则会 TLE
memset(a, 0, sizeof(long long) * (n + 1));
memset(b, 0, sizeof(long long) * (n + 1));
}
int main()
{
T = read();
while (T--){
C();
n = read();
if (n % 2 == 1) { //n为奇数
for (int i = 1; i <= n; ++i)
a[i] = read();
ll h = 1, t = n, cnt = -1;
while (1) { //构造 b 数列
if (h == t){
b[h] = ++cnt;
break;
}
b[h] = b[t] = ++cnt;
h++, t--;
}
bool ok = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] < b[i]) {
puts("No");
ok = 1;
break;
}
}
if (ok == 0)
puts("Yes");
}
else { //n为偶数
int sb;
for (int i = 1; i <= n; ++i)
a[i] = read();
ll h = 1, t = n, cnt = -1;
while (1) { //构造 b 数列
if (h + 1 == t) {
b[h] = b[t] = ++cnt;
sb = h;
b[h]++;
break;
}
b[h] = b[t] = ++cnt;
h++, t--;
}
bool ok = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] < b[i]) {
ok = 1;
break;
}
}
if (ok == 0) {
puts("Yes");
continue;
}
b[sb]--; //换成第二个 b 数列
b[sb + 1]++;
ok = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] < b[i]) {
puts("No");
ok = 1;
break;
}
}
if (ok == 0)
puts("Yes");
}
}
return 0;
}