QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#734732 | #1873. Array | zzxLLL | AC ✓ | 330ms | 27280kb | C++14 | 4.5kb | 2024-11-11 14:40:24 | 2024-11-11 14:40:25 |
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("-1\n"), void();
if (!b[n]) return printf("-1\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("-1\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("-1\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: 100
Accepted
time: 5ms
memory: 21608kb
input:
3 4 3 3 5 5 7 4 6 6 7 8 8 8 5 2 3 4 4 6
output:
1 1 2 2 3 4 2 1 4 3 2 -1
result:
ok 233
Test #2:
score: 0
Accepted
time: 195ms
memory: 25988kb
input:
23198 7 1 2 3 4 5 6 7 7 1 2 3 4 5 6 8 7 1 2 3 4 5 7 7 7 1 2 3 4 5 7 8 7 1 2 3 4 5 8 8 7 1 2 3 4 6 6 7 7 1 2 3 4 6 6 8 7 1 2 3 4 6 7 7 7 1 2 3 4 6 7 8 7 1 2 3 4 6 8 8 7 1 2 3 4 7 7 7 7 1 2 3 4 7 7 8 7 1 2 3 4 7 8 8 7 1 2 3 4 8 8 8 7 1 2 3 5 5 6 7 7 1 2 3 5 5 6 8 7 1 2 3 5 5 7 7 7 1 2 3 5 5 7 8 7 1 2 ...
output:
1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
ok 233
Test #3:
score: 0
Accepted
time: 151ms
memory: 22076kb
input:
2000 1000 964 987 987 989 992 999 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1...
output:
6 4 2 5 3 1 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 ...
result:
ok 233
Test #4:
score: 0
Accepted
time: 156ms
memory: 25056kb
input:
16 100000 44700 98571 99279 99995 99998 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 1...
output:
5 6 7 4 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ...
result:
ok 233
Test #5:
score: 0
Accepted
time: 155ms
memory: 24412kb
input:
1006 1000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok 233
Test #6:
score: 0
Accepted
time: 330ms
memory: 24288kb
input:
106 20000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok 233
Test #7:
score: 0
Accepted
time: 221ms
memory: 27280kb
input:
58796 10 1 2 3 4 5 6 7 8 9 10 10 1 2 3 4 5 6 7 8 9 11 10 1 2 3 4 5 6 7 8 10 10 10 1 2 3 4 5 6 7 8 10 11 10 1 2 3 4 5 6 7 8 11 11 10 1 2 3 4 5 6 7 9 9 10 10 1 2 3 4 5 6 7 9 9 11 10 1 2 3 4 5 6 7 9 10 10 10 1 2 3 4 5 6 7 9 10 11 10 1 2 3 4 5 6 7 9 11 11 10 1 2 3 4 5 6 7 10 10 10 10 1 2 3 4 5 6 7 10 10...
output:
1 1 1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
ok 233