#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define ll long long
const int mxn=1e5+5;
int n,a[mxn],b[mxn],k,L[mxn],R[mxn],sz[mxn],mx[mxn];
vector<int>g[mxn],par[mxn];
set<int>s;
void solve(int m){
if(s.size()==0 && m<n+1){
cout<<-1;
exit(0);
}
if(m==n+1){
k--;
if(k==0){
for(int i=1;i<=n;i++)
cout<<a[i]<<" ";
exit(0);
}
return ;
}
vector<int>v;
for(auto i:s){
v.push_back(i);
if(v.size()==10)
break;
}
for(auto i:v){
a[i]=m;
s.erase(i);
for(auto j:par[i]){
sz[j]--;
if(sz[j]==0)
s.insert(j);
}
solve(m+1);
s.insert(i);
for(auto j:par[i]){
sz[j]++;
if(sz[j]==1)
s.erase(j);
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
int cnt=0;
vector<int>hey;
for(int i=1;i<=n;i++){
cin>>b[i];
if(b[i]==-1)hey.push_back(i);
}
if(b[n]!=-1){
cout<<-1;
return 0;
}
for(int i=1;i<hey.size();i++){
L[hey[i]]=hey[i-1];
}
for(int i=0;i<hey.size();i++){
int l=(i==0)?1:hey[i-1]+1;
vector<int>v;
for(;l<hey[i];l++){
while(v.size() && b[v.back()]<=b[l]){
R[v.back()]=l;
v.pop_back();
}
if(v.size())
L[l]=v.back();
v.push_back(l);
}
while(v.size()){
R[v.back()]=hey[i];
v.pop_back();
}
}
for(int i=1;i<=n;i++){
if(L[i]!=0){
par[i].push_back(L[i]);
g[L[i]].push_back(i);
mx[L[i]]=max(mx[L[i]],b[i]);
}
if(R[i]!=0){
par[i].push_back(R[i]);
g[R[i]].push_back(i);
}
}
for(int i=1;i<=n;i++){
// cout<<L[i]<<" "<<R[i]<<'\n';
if(b[i]!=-1 && mx[i]+1!=b[i]){
cout<<-1;
return 0;
}
}
for(int i=1;i<=n;i++){
sz[i]=g[i].size();
if(sz[i]==0)
s.insert(i);
}
solve(1);
cout<<-1;
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:65:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i=1;i<hey.size();i++){
| ~^~~~~~~~~~~
Main.cpp:68:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i=0;i<hey.size();i++){
| ~^~~~~~~~~~~
Main.cpp:55:6: warning: unused variable 'cnt' [-Wunused-variable]
55 | int cnt=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Incorrect |
1 ms |
6760 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Incorrect |
1 ms |
6760 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
21868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
21868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Incorrect |
1 ms |
6760 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |