题解:AT_abc349_g [ABC349G] Palindrome Construction
直接转化,题目意思就是对于
但是这样复杂度不对。考虑区间并查集连边,我们有套路萌萌哒可以倍增区间连边在均摊
但是我们还可以考虑回文串的性质,即对称,也就是说根据对称性,我们对每个点最多连
最后,由于我们省去了
// 这回只花了 1s 就打完了。
// 真好。记得多手造几组。最好有暴力对拍。
#include <bits/stdc++.h>
#define int long long
#define upp(a, x, y) for (int a = x; a <= y; a++)
#define dww(a, x, y) for (int a = x; a >= y; a--)
#define pb(x) push_back(x)
#define endl '\n'
#define x first
#define y second
#define PII pair<int, int>
using namespace std;
const int N = 2e5 + 10;
int n, d[N], p[N], num[N];
vector<int> op[N];
int find(int x) {
if (p[x] != x) return p[x] = find(p[x]);
return x;
}
void solve() {
cin >> n;
upp(i, 1, n) cin >> d[i];
upp(i, 1, n) p[i] = i;
int l = 0, r = 0;
upp(i, 1, n) {
if (i > r) {
upp(j, 0, d[i]) p[find(i + j)] = find(i - j);
} else {
if (d[i] < min(d[l + r - i], r - i)) {
cout << "No" << endl;
return;
}
if (d[i] > min(d[l + r - i], r - i)) {
upp(j, min(d[l + r - i], r - i), d[i]) {
p[find(i + j)] = find(i - j);
}
}
}
if (i + d[i] > r) r = i + d[i], l = i - d[i];
}
upp(i, 1, n) {
int x = i - d[i] - 1, y = i + d[i] + 1;
if (x > 0 && y <= n) {
x = find(x), y = find(y);
if (x == y) {
cout << "No" << endl;
return;
}
op[x].pb(y);
op[y].pb(x);
}
}
upp(i, 1, n) {
if (!num[find(i)]) {
unordered_map<int, int> ha;
upp(j, 0, (int)op[find(i)].size() - 1) {
ha[num[op[find(i)][j]]] = 1;
}
int now = 1;
while (ha[now]) now++;
num[find(i)] = now;
}
}
cout << "Yes" << endl;
upp(i, 1, n) cout << num[find(i)] << ' ';
cout << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int tt = 1;
while (tt--) solve();
return 0;
}