QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#723016 | #1873. Array | zhenjianuo2025 | AC ✓ | 192ms | 19192kb | C++14 | 3.0kb | 2024-11-07 20:56:05 | 2024-11-07 20:56:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define piii tuple<int,int,int>
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define fi first
#define se second
#define era erase
#define ins insert
#define it iterator
#define lb lower_bound
#define ub upper_bound
#define exc(exp) if(exp)continue;
#define ret(exp) if(exp)return;
#define stop(exp) if(exp)break;
#define quit(sth) {sth;return;}
#define let(var...) int var;tie(var)
#define siz(vec) ((int)((vec).size()))
#define all(vec) (vec).begin(),(vec).end()
#define unq(vec) sort(all(vec)),(vec).erase(unique(all(vec)),(vec).end())
#define deb(var) cerr<<#var<<'='<<(var)<<"; "
#define debl(var) cerr<<#var<<'='<<(var)<<";\n"
#define int long long
#define inf (long long)(1e18)
mt19937 gen(random_device{}());
void Max(int &x,int y){if(x<y)x=y;}
void Min(int &x,int y){if(x>y)x=y;}
const int mod=998244353;
void Add(int &x,int y){x=x+y<mod?x+y:x+y-mod;}
int add(int x,int y){return x+y<mod?x+y:x+y-mod;}
int fpm(int x,int y){
int ans=1;for(;y;y>>=1,(x*=x)%=mod)if(y&1)(ans*=x)%=mod;return ans;
}
int n,a[500010],b[500010];
int cnt[500010];
bool chk(){
fill(cnt,cnt+n+1,0);
int al=0;
for(int i=1;i<=n;i++)al+=!cnt[a[i]],++cnt[a[i]];
fill(cnt,cnt+n+1,0);
for(int i=1,j=0,c=0;i<=n;i++){
while(j<=n&&c<al){
++j;
c+=!(cnt[a[j]]++);
}
if(b[i]!=j)return 0;
//return deb(i),deb(j),debl(b[i]),0;
c-=!(--cnt[a[i]]);
} return 1;
}
int fa[500010];
int find(int u){
return fa[u]==u?u:fa[u]=find(fa[u]);
}
int lst[500010];
void work(){
cin>>n;
for(int i=1;i<=n;i++)cin>>b[i];b[n+1]=n+1;
for(int i=1;i<=n;i++)
if(b[i]<b[i-1])quit(cout<<"-1\n");
fill(lst+1,lst+n+1,0);
int LIM=n,CNT=1;
for(int i=1;i<=n;i++)
if(b[i]!=n+1)Min(LIM,b[i]-i+1);
// debl(LIM);
queue<int> q;
for(int i=1;i<=n+1;i++){
if(b[i]>b[i-1]){
lst[b[i]]=i-1;
for(int j=b[i-1]+1;j<b[i];j++){
if(CNT<LIM)++CNT;
else{
if(!q.empty())lst[j]=q.front(),q.pop();
else lst[j]=j-1;
}
if(b[j]==b[j+1])q.push(j);
}
if(b[b[i]]==b[b[i]+1])q.push(b[i]);
}
}
for(int i=1,c=0;i<=n;i++){
a[i]=lst[i]?a[lst[i]]:++c;
// deb(i),cerr<<(a[i])<<'\n';
}
// cerr<<'\n';
if(!chk())cout<<"-1\n";
else{
for(int i=1;i<=n;i++)cout<<a[i]<<" \n"[i==n];
}
}
signed main(){
// freopen("sing.in","r",stdin);
// freopen("sing.out","w",stdout);
ios::sync_with_stdio(0),
cin.tie(0),cout.tie(0);
int T=1;cin>>T;while(T--)work();
}
/*
* CONTINUE, NON-STOPPING, FOR THE FAITH
* START TYPING IF YOU DON'T KNOW WHAT TO DO
* STOP TYPING IF YOU DON'T KNOW WHAT YOU'RE DOING
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9692kb
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 1 2 3 4 2 1 3 -1
result:
ok 233
Test #2:
score: 0
Accepted
time: 160ms
memory: 15920kb
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: 118ms
memory: 9668kb
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:
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 100 101 102 ...
result:
ok 233
Test #4:
score: 0
Accepted
time: 118ms
memory: 14720kb
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:
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 100 101 102 ...
result:
ok 233
Test #5:
score: 0
Accepted
time: 111ms
memory: 10888kb
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: 192ms
memory: 16300kb
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: 158ms
memory: 19192kb
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