QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#733948 | #1873. Array | complexor | AC ✓ | 411ms | 12784kb | C++23 | 2.4kb | 2024-11-10 22:19:15 | 2024-11-10 22:19:16 |
Judging History
answer
#include <bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ULL;
typedef std::pair<int, int> pii;
#define MP std::make_pair
#define fi first
#define se second
int read()
{
int s = 0, f = 1;
char c = getchar();
while (!(c >= '0' && c <= '9'))
f ^= (c == '-'), c = getchar();
while (c >= '0' && c <= '9')
s = s * 10 + (c - '0'), c = getchar();
return f ? s : -s;
}
const int MAXN = 500005, inf = 0x3f3f3f3f;
int n, rb[MAXN], nxt[MAXN], a[MAXN];
int used[MAXN];
int cnt[MAXN], tot, all;
void insert(int x) { if (!cnt[x]++) ++tot; }
void erase(int x){ if (!--cnt[x]) --tot; }
bool chk()
{
memset(cnt + 1, 0, n << 2), tot = 0;
for (int i = 1; i <= n; i++) insert(a[i]);
all = tot;
memset(cnt + 1, 0, n << 2), tot = 0;
for (int i = 1, j = 1; i <= n; i++)
{
while (tot < all && j <= n + 1) insert(a[j++]);
if (j - 1 != rb[i]) return false;
erase(a[i]);
}
return true;
}
bool chk(int cnt)
{
if (cnt < 0) return false;
memset(used + 1, false, n << 2);
for (int i = 1; i < n; i++)
if (rb[i] < rb[i + 1])
used[nxt[i] = rb[i + 1]]++;
nxt[n] = n + 1;
for (int i = 1; i < n; i++) used[rb[i]]++;
for (int i = n - 1, j = n; i; i--)
if (rb[i] == rb[i + 1])
{
if (rb[i] <= i + 1) return false;
if (cnt) { nxt[i] = n + 1, --cnt; continue; }
j = std::min(j, rb[i] - 1);
while (used[j] && j >= i + 1) j--;
if (j <= i) return false;
used[nxt[i] = j]++, used[rb[i]]--;
}
int v = 0;
for (int i = 1; i <= n; i++)
if (nxt[i] == n + 1) a[i] = ++v;
for (int i = n; i; i--)
if (nxt[i] <= n) a[i] = a[nxt[i]];
return true;
}
int main()
{
// freopen("sing.in", "r", stdin);
// freopen("sing.out", "w", stdout);
for (int T = read(); T--; )
{
n = read();
for (int i = 1; i <= n; i++) rb[i] = read();
int seg = 0;
for (int i = 1; i < n; i++) seg += rb[i] == rb[i + 1];
int l = -1, r = seg;
bool f = rb[1] <= n;
for (int i = 1; i <= n; i++)
f &= rb[i] >= i && (i == n || rb[i] <= rb[i + 1]);
if (!f) { printf("-1\n"); goto end; }
while (l < r)
{
int mid = (l + r) >> 1;
if (chk(mid)) r = mid;
else l = mid + 1;
}
if (l == -1) { printf("-1\n"); continue; }
chk(l);
if (chk())
{
// printf("YES\n");
for (int i = 1; i <= n; i++)
printf("%d%c", a[i], " \n"[i == n]);
}
else printf("-1\n");
end:;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11964kb
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 2 4 1 2 3 4 -1
result:
ok 233
Test #2:
score: 0
Accepted
time: 369ms
memory: 12436kb
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: 210ms
memory: 12020kb
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:
3 5 7 4 6 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 ...
result:
ok 233
Test #4:
score: 0
Accepted
time: 263ms
memory: 12344kb
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:
4 3 2 5 1 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 ...
result:
ok 233
Test #5:
score: 0
Accepted
time: 234ms
memory: 10616kb
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: 411ms
memory: 12352kb
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: 387ms
memory: 12784kb
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