#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],par[mxn];
vector<int>g[mxn];
set<int>s;
void solve(int m){
if(k<=0 || (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);
sz[par[i]]--;
if(sz[par[i]]==0)
s.insert(par[i]);
solve(m+1);
s.insert(i);
sz[par[i]]++;
if(sz[par[i]]==1)
s.erase(par[i]);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
int cnt=0;
for(int i=1;i<=n;i++){
cin>>b[i];
}
if(b[n]!=-1){
cout<<-1;
return 0;
}
for(int i=n;i>=1;i--){
if(b[i]==-1){
b[i]=n+cnt;
cnt++;
}
}
vector<int>v;
for(int i=1;i<=n;i++){
while(v.size() && b[v.back()]<=b[i]){
R[v.back()]=i;
v.pop_back();
}
if(v.size())
L[i]=v.back();
v.push_back(i);
}
for(int i=1;i<=n;i++){
if(L[i]==0 || R[i]==0){
par[i]=max(R[i],L[i]);
g[max(R[i],L[i])].push_back(i);
}
else if(b[L[i]]<=b[R[i]]){
par[i]=L[i];
g[L[i]].push_back(i);
}
else{
par[i]=R[i];
g[R[i]].push_back(i);
}
//cout<<L[i]<<" "<<R[i]<<'\n';
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
18012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
18012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |