QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#734727 | #1873. Array | zzxLLL | WA | 0ms | 21720kb | C++14 | 4.5kb | 2024-11-11 14:39:03 | 2024-11-11 14:39:04 |
Judging History
answer
#include <bits/stdc++.h>
inline int read() {
char ch = getchar();
int x = 0;
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x;
}
// namespace Solve {
// const int M = 5e5 + 10;
// int n, b[M], nxt[M];
// int ans[M];
// void mian() {
// n = read();
// for (int i = 1; i <= n; i++) b[i] = read();
// for (int i = 1; i < n; i++)
// if (b[i] > b[i + 1]) return printf("NO\n"), void();
// if (b[1] > n) return printf("NO\n"), void();
// int mn = n;
// for (int i = 1; i <= n; i++)
// if (b[i] <= n) mn = std::min(mn, b[i] - i + 1);
// b[n + 1] = n + 2;
// for (int i = 1; i <= n; i++) ans[i] = 0;
// for (int i = 1; i <= n; i++)
// static int pre[M];
// std::multiset<int> S;
// for (int i = 1; i <= cnt; i++) pre[i] = n + 1;
// for (int i = 1; i <= cnt; i++) S.emplace(n + 1);
// for (int i = n; i >= 1; i--) {
// S.erase(S.find(pre[ans[i]]));
// S.emplace(pre[ans[i]] = i);
// if (*S.rbegin() != b[i]) return printf("NO\n"), void();
// }
// printf("YES\n");
// for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
// putchar('\n');
// }
// }
namespace Solve {
const int M = 5e5 + 10;
int n, b[M], a[M], pre[M], f[M];
std::vector<std::pair<int, int> > vec[M];
void mian() {
// 第 i 次操作:选择 x[i],f[x[i]] <- i
// 要求 min(f) = b[i]
// b[i] 单调不减
// 对于 x = b[i] = b[i + 1] = ... = b[j] != b[j + 1] = y
// 保留一个 x
// 剩下 (t - 1) 个数,在 i 时刻,为 i - t + 2, i - t + 3, ..., i
// (t - 1) 个数轮换出现
// 时刻 y 时,保留一个,剩下的数字继续轮换出现
// 在 最小值为 a 的时刻 [l, r],有 u 种 b[i] \in [l, r]
// 若不存在 b[i] = r 则至少需要 u + 2 种数字
// 否则至少需要 u + 1 种数字
n = read();
for (int i = 1; i <= n; i++) b[n - i + 1] = n - read() + 1;
// printf("b: "); for (int i = 1; i <= n; i++) printf("%d ", b[i]); putchar('\n');
for (int i = 0; i <= n; i++) pre[i] = 0;
for (int i = 2; i <= n; i++)
if (b[i] < b[i - 1]) return printf("NO\n"), void();
if (!b[n]) return printf("NO\n"), void();
for (int i = 1; i <= n; i++) pre[b[i]] = 1;
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + pre[i];
int lim = 1;
for (int i = 1; i <= n; i++) {
int j = i;
while (j < n && b[j] == b[j + 1]) ++j;
// printf("[%d, %d]\n", i, j);
lim = std::max(lim, pre[j] - pre[b[i]] + 1
+ (j > i && pre[j] <= pre[j - 1])); // 需要特判 |S(A[1,n])| = 1 吗?好像要。
i = j;
}
// printf("lim = %d\n", lim);
std::multiset<int> F;
std::set<std::pair<int, int> > S;
for (int i = 0; i <= n + 1; i++) vec[i].clear();
for (int i = 1; i <= lim; i++) F.emplace(f[i] = 0);
for (int i = 1; i <= lim; i++) S.emplace(0, i);
// if (b[1] <= 0) {
// vec[std::upper_bound(b + 1, b + 1 + n, 0) - b].emplace_back(0, 1);
// F.erase(F.find(0)), S.erase(std::make_pair(0, 1));
// }
for (int i = 0; i <= n; i++) {
while (!vec[i].empty())
S.emplace(vec[i].back()), vec[i].pop_back();
if (S.empty()) return printf("NO\n"), void();
// auto [pos, val] = *(++S.lower_bound()); S.erase(*S.begin());
auto it = S.begin();
auto [pos, val] = *it; S.erase(it);
F.erase(F.find(f[val])), F.emplace(f[val] = i);
a[i] = val;
// printf("a[%d] = %d, val = %d\n", i, a[i], val);
if (!i || pre[i] > pre[i - 1]) {
int x = std::upper_bound(b + 1, b + 1 + n, i) - b;
vec[x].emplace_back(i, val);
} else {
S.emplace(f[val] = i, val);
}
if (*F.begin() != b[i]) return printf("NO\n"), void();
}
printf("YES\n");
for (int i = n; i >= 1; i--) printf("%d ", a[i]);
putchar('\n');
return ;
}
}
int main() {
int T = read();
while (T--) /* printf("T = %d\n"), */Solve::mian();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 21720kb
input:
3 4 3 3 5 5 7 4 6 6 7 8 8 8 5 2 3 4 4 6
output:
YES 1 1 2 2 YES 3 4 2 1 4 3 2 NO
result:
wrong output format Expected integer, but "YES" found