#include <bits/stdc++.h>
//#pragma GCC optimize ("Ofast,unroll-loops")
//#pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
#define ff first
#define ss second
void dbg(){
cerr << endl;
}
template<typename Head, typename... Tail> void dbg(Head h, Tail... t){
cerr << " " << h << ",";
dbg(t...);
}
#define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">:", dbg(__VA_ARGS__)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
// #ifndef ONLINE_JUDGE
// freopen("inp.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// #endif
}
const ll mod = 1e9 + 7, maxn = 1e5 + 5, lg = 20, inf = ll(1e9) + 5;
vector<int> adj[maxn];
int cnt[maxn], n;
int fen[maxn];
void upd(int i, int k){
for(i++; i < maxn; i += i&-i) fen[i] = max(fen[i], k);
}
int get(int i){
int res = 0;
for(i++; i; i -= i&-i) res = max(res, fen[i]);
return res;
}
vector<int> val(vector<int> p){
vector<int> res(n);
vector<int> stk;
per(i,n-1,0){
while(sz(stk) && p[stk.back()] < p[i]) stk.pop_back();
if(sz(stk)){
res[i] = get(stk.back()-1) + 1;
}else res[i] = inf;
upd(i, res[i]);
stk.pb(i);
}
int tmp = n;
rep(i,0,n) res[i] += res[i] == inf? tmp--: 0;
return res;
}
vector<int> nxt(vector<int> p){
vector<int> pos(n);
rep(i,0,n) pos[p[i]] = i;
rep(i,0,n) cnt[i] = 0;
set<pp> s;
int find = -1;
per(i,n-1,0){
s.insert({0, pos[i]});
for(int c: adj[pos[i]]){
s.erase(pp(cnt[c], c));
s.insert(pp(++cnt[c], c));
}
auto it = s.upper_bound(pp(0, pos[i]));
if(it != end(s) && it->ff == 0){
find = i + 1;
p[it->ss] = i;
s.erase(it);
break;
}
}
if(find + 1){
while(sz(s)){
auto[_, r] = *begin(s);
s.erase(begin(s));
p[r] = find++;
for(int c: adj[r]){
s.erase(pp(cnt[c], c));
s.insert(pp(--cnt[c], c));
}
}
return p;
}
return {};
}
int main(){ IOS();
int k; cin >> n >> k;
vector<int> a(n);
int tmp = n;
rep(i,0,n) {
cin >> a[i];
if(a[i] == -1){
a[i] = inf + tmp, tmp--;
}
}
vector<int> stk;
rep(i,0,n){
while(sz(stk) && a[stk.back()] < a[i]) stk.pop_back();
if(sz(stk)){
if(a[i] - a[stk.back()]){
adj[i].pb(stk.back());
cnt[stk.back()]++;
}
}
stk.pb(i);
}
stk.clear();
per(i,n-1,0){
while(sz(stk) && a[stk.back()] < a[i]) stk.pop_back();
if(sz(stk)){
adj[i].pb(stk.back());
cnt[stk.back()]++;
}
stk.pb(i);
}
set<pp> s;
rep(i,0,n) s.insert({cnt[i], i});
vector<int> ans(n);
int ptr = 0;
while(sz(s) && begin(s)->ff == 0){
auto[_, r] = *begin(s);
s.erase(begin(s));
ans[r] = ptr++;
for(int c: adj[r]){
s.erase(pp(cnt[c], c));
s.insert(pp(--cnt[c], c));
}
}
if(sz(s) || val(ans) != a){
cout << -1 << '\n';
} else{
k--;
while(k--){
ans = nxt(ans);
if(ans.empty()) return cout << -1 << '\n', 0;
}
for(int c: ans) cout << ++c << ' '; cout << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:155:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
155 | for(int c: ans) cout << ++c << ' '; cout << '\n';
| ^~~
Main.cpp:155:45: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
155 | for(int c: ans) cout << ++c << ' '; cout << '\n';
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2684 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2684 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
7756 KB |
Output is correct |
2 |
Correct |
42 ms |
7564 KB |
Output is correct |
3 |
Correct |
43 ms |
7772 KB |
Output is correct |
4 |
Correct |
44 ms |
7628 KB |
Output is correct |
5 |
Correct |
41 ms |
7680 KB |
Output is correct |
6 |
Correct |
42 ms |
7680 KB |
Output is correct |
7 |
Correct |
42 ms |
7720 KB |
Output is correct |
8 |
Correct |
42 ms |
7756 KB |
Output is correct |
9 |
Correct |
43 ms |
7664 KB |
Output is correct |
10 |
Correct |
42 ms |
7732 KB |
Output is correct |
11 |
Correct |
89 ms |
12848 KB |
Output is correct |
12 |
Correct |
92 ms |
12748 KB |
Output is correct |
13 |
Correct |
89 ms |
12748 KB |
Output is correct |
14 |
Correct |
92 ms |
12772 KB |
Output is correct |
15 |
Correct |
97 ms |
12620 KB |
Output is correct |
16 |
Correct |
91 ms |
12620 KB |
Output is correct |
17 |
Correct |
94 ms |
12608 KB |
Output is correct |
18 |
Correct |
94 ms |
12656 KB |
Output is correct |
19 |
Correct |
93 ms |
12588 KB |
Output is correct |
20 |
Correct |
95 ms |
12532 KB |
Output is correct |
21 |
Correct |
94 ms |
12592 KB |
Output is correct |
22 |
Correct |
92 ms |
12612 KB |
Output is correct |
23 |
Correct |
91 ms |
12492 KB |
Output is correct |
24 |
Correct |
86 ms |
13280 KB |
Output is correct |
25 |
Correct |
79 ms |
13268 KB |
Output is correct |
26 |
Correct |
76 ms |
13244 KB |
Output is correct |
27 |
Correct |
72 ms |
13132 KB |
Output is correct |
28 |
Correct |
73 ms |
13208 KB |
Output is correct |
29 |
Correct |
73 ms |
13168 KB |
Output is correct |
30 |
Correct |
22 ms |
5736 KB |
Output is correct |
31 |
Correct |
35 ms |
7744 KB |
Output is correct |
32 |
Correct |
62 ms |
11908 KB |
Output is correct |
33 |
Correct |
68 ms |
13000 KB |
Output is correct |
34 |
Correct |
7 ms |
3668 KB |
Output is correct |
35 |
Correct |
17 ms |
4692 KB |
Output is correct |
36 |
Correct |
32 ms |
7780 KB |
Output is correct |
37 |
Correct |
77 ms |
12900 KB |
Output is correct |
38 |
Correct |
82 ms |
12900 KB |
Output is correct |
39 |
Correct |
73 ms |
13024 KB |
Output is correct |
40 |
Correct |
84 ms |
13024 KB |
Output is correct |
41 |
Correct |
73 ms |
13552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
7756 KB |
Output is correct |
2 |
Correct |
42 ms |
7564 KB |
Output is correct |
3 |
Correct |
43 ms |
7772 KB |
Output is correct |
4 |
Correct |
44 ms |
7628 KB |
Output is correct |
5 |
Correct |
41 ms |
7680 KB |
Output is correct |
6 |
Correct |
42 ms |
7680 KB |
Output is correct |
7 |
Correct |
42 ms |
7720 KB |
Output is correct |
8 |
Correct |
42 ms |
7756 KB |
Output is correct |
9 |
Correct |
43 ms |
7664 KB |
Output is correct |
10 |
Correct |
42 ms |
7732 KB |
Output is correct |
11 |
Correct |
89 ms |
12848 KB |
Output is correct |
12 |
Correct |
92 ms |
12748 KB |
Output is correct |
13 |
Correct |
89 ms |
12748 KB |
Output is correct |
14 |
Correct |
92 ms |
12772 KB |
Output is correct |
15 |
Correct |
97 ms |
12620 KB |
Output is correct |
16 |
Correct |
91 ms |
12620 KB |
Output is correct |
17 |
Correct |
94 ms |
12608 KB |
Output is correct |
18 |
Correct |
94 ms |
12656 KB |
Output is correct |
19 |
Correct |
93 ms |
12588 KB |
Output is correct |
20 |
Correct |
95 ms |
12532 KB |
Output is correct |
21 |
Correct |
94 ms |
12592 KB |
Output is correct |
22 |
Correct |
92 ms |
12612 KB |
Output is correct |
23 |
Correct |
91 ms |
12492 KB |
Output is correct |
24 |
Correct |
86 ms |
13280 KB |
Output is correct |
25 |
Correct |
79 ms |
13268 KB |
Output is correct |
26 |
Correct |
76 ms |
13244 KB |
Output is correct |
27 |
Correct |
72 ms |
13132 KB |
Output is correct |
28 |
Correct |
73 ms |
13208 KB |
Output is correct |
29 |
Correct |
73 ms |
13168 KB |
Output is correct |
30 |
Correct |
22 ms |
5736 KB |
Output is correct |
31 |
Correct |
35 ms |
7744 KB |
Output is correct |
32 |
Correct |
62 ms |
11908 KB |
Output is correct |
33 |
Correct |
68 ms |
13000 KB |
Output is correct |
34 |
Correct |
7 ms |
3668 KB |
Output is correct |
35 |
Correct |
17 ms |
4692 KB |
Output is correct |
36 |
Correct |
32 ms |
7780 KB |
Output is correct |
37 |
Correct |
77 ms |
12900 KB |
Output is correct |
38 |
Correct |
82 ms |
12900 KB |
Output is correct |
39 |
Correct |
73 ms |
13024 KB |
Output is correct |
40 |
Correct |
84 ms |
13024 KB |
Output is correct |
41 |
Correct |
73 ms |
13552 KB |
Output is correct |
42 |
Incorrect |
42 ms |
7544 KB |
Output isn't correct |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2684 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |