This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define debug(x) cout << #x << "= " << x << ", "
#define ll long long
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define SZ(x) (int) x.size()
#define wall cout << endl;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 10;
int n , k , ar[maxn] , deg[maxn] , C , fbi[maxn];
vector <int> adj[maxn];
set <int> cand;
vector <int> topol , ans;
void solve()
{
if(cand.empty())
{
C++;
if(C == k)
{
ans = topol;
}
return;
}
vector <int> tmp;
for(auto v : cand)
{
tmp.pb(v);
if(SZ(tmp) == k)
break;
}
for(auto v : tmp)
{
cand.erase(v);
topol.pb(v);
for(auto u : adj[v])
{
deg[u]--;
if(deg[u] == 0)
cand.insert(u);
}
solve();
if(C == k)
break;
for(auto u : adj[v])
{
deg[u]++;
if(deg[u] == 1) cand.erase(u);
}
cand.insert(v);
topol.pop_back();
}
}
int32_t main()
{
fast;
cin >> n >> k;
for(int i = 1 ; i <= n ; i++) cin >> ar[i];
if(ar[n] != -1)
{
cout << -1 << endl;
return 0;
}
vector <int> st;
st.pb(n);
bool ok = true;
for(int i = n - 1 ; i > 0 ; i--)
{
int mx = 0;
if(ar[i] == -1)
{
while(!st.empty())
{
auto now = st.back();
adj[now].pb(i);
deg[i]++;
st.pop_back();
}
st.pb(i);
}
else
{
while(!st.empty())
{
auto now = st.back();
if(ar[now] == -1 || ar[now] >= ar[i])
{
if(ar[i] != mx + 1) ok = false;
adj[i].pb(now);
deg[now]++;
break;
}
mx = max(mx , ar[now]);
adj[now].pb(i);
deg[i]++;
st.pop_back();
}
st.pb(i);
}
}
vector <int> vec;
if(!ok)
{
cout << -1 << endl;
return 0;
}
for(int i = 1 ; i <= n ; i++) if(deg[i] == 0) cand.insert(i);
solve();
if(C < k)
{
cout << -1 << endl;
return 0;
}
for(int i = 0 ; i < n ; i++)
fbi[ans[i]] = i + 1;
for(int i = 1 ; i <= n ; i++)
cout << fbi[i] << " ";
cout << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |