Submission #624249

# Submission time Handle Problem Language Result Execution time Memory
624249 2022-08-07T14:50:45 Z Arinoor Watermelon (INOI20_watermelon) C++17
100 / 100
145 ms 9656 KB
#include <bits/stdc++.h>
using namespace std;

#define pb				push_back
#define mp				make_pair
#define fi				first
#define se				second
#define all(x)			x.begin(), x.end()
#define bug(x)			cout << #x << " : " << x << '\n'
#define ios				ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)

typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 1e5 + 10;
const int maxlg = 17;
const int mod = 1e9 + 7;
const int inf = 1e9 + 10;

int n, a[maxn], b[maxn], d[maxn], D[maxn], t[maxn];
int vis[maxn];
int fen[maxn];
vector<int> G[maxn];

void dfs(int v){
	vis[v] = 1;
	for(int u : G[v]){
		if(!vis[u])
			dfs(u);
		else if(vis[u] == 1){
			cout << "-1\n";
			exit(0);
		}
	}
	vis[v] = 2;
}

void add(int i, int x){
	for(; i <= n; i += i & -i)
		fen[i] += x;
}

int get(int t){
	int x = 0;
	for(int i = maxlg - 1; ~i; i --){
		int j = x + (1 << i);
		if(j <= n and fen[j] < t){
			x = j;
			t -= fen[j];
		}
	}
	return x + 1;
}

int main(){
	ios;
	int k;
	cin >> n >> k;
	for(int i = 1; i <= n; i ++)
		cin >> b[i];
	if(~b[n])
		return cout << "-1\n", 0;
	vector<pii> S;
	for(int i = n; i; i --){
		if(b[i] == -1){
			if(!S.empty()){
				G[S.front().fi].pb(i);
				d[i] ++;
			}
			S.clear();
			S.pb(mp(i, inf));
		}
		else{
			pii last = mp(0, 0);
			while(S.back().se < b[i]){
				last = S.back();
				S.pop_back();
			}
			if(b[i] != last.se + 1)
				return cout << "-1\n", 0;
			if(last.fi){
				G[last.fi].pb(i);
				d[i] ++;
			}
			G[i].pb(S.back().fi);
			d[S.back().fi] ++;
			if(S.back().se == b[i]){
				S.pop_back();
			}
			S.pb(mp(i, b[i]));
		}
	}
	for(int i = 1; i <= n; i ++)
		if(!vis[i])
			dfs(i);
	for(int i = 1; i <= n; i ++)
		t[i] = 1;
	for(int i = 1; i <= k; i ++){
		memset(fen, 0, sizeof fen);
		int last = 0, cnt = 0;
		for(int i = 1; i <= n; i ++){
			D[i] = d[i];
			if(!D[i]){
				add(i, 1);
				cnt ++;
			}
		}
		for(int j = 1; j <= n; j ++){
			int x = get(t[j]);
			if(cnt > t[j])
				last = j;
			add(x, -1);
			cnt --;
			a[x] = j;
			for(int u : G[x]){
				if(!(--D[u])){
					add(u, 1);
					cnt ++;
				}
			}
		}
		if(i == k){
			for(int j = 1; j <= n; j ++)
				cout << a[j] << ' ';
			return cout << '\n', 0;
		}
		if(!last)
			return cout << "-1\n", 0;
		t[last] ++;
		for(int j = last + 1; j <= n; j ++)
			t[j] = 1;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 1 ms 3028 KB Output is correct
3 Correct 3 ms 3028 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2664 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 3028 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 2 ms 3064 KB Output is correct
15 Correct 3 ms 3028 KB Output is correct
16 Correct 2 ms 3028 KB Output is correct
17 Correct 2 ms 3028 KB Output is correct
18 Correct 2 ms 3028 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2680 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 3 ms 3068 KB Output is correct
26 Correct 2 ms 3028 KB Output is correct
27 Correct 2 ms 3028 KB Output is correct
28 Correct 2 ms 3028 KB Output is correct
29 Correct 2 ms 3068 KB Output is correct
30 Correct 1 ms 3028 KB Output is correct
31 Correct 2 ms 3028 KB Output is correct
32 Correct 2 ms 3028 KB Output is correct
33 Correct 2 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 1 ms 3028 KB Output is correct
3 Correct 3 ms 3028 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2664 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 3028 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 2 ms 3064 KB Output is correct
15 Correct 3 ms 3028 KB Output is correct
16 Correct 2 ms 3028 KB Output is correct
17 Correct 2 ms 3028 KB Output is correct
18 Correct 2 ms 3028 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2680 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 3 ms 3068 KB Output is correct
26 Correct 2 ms 3028 KB Output is correct
27 Correct 2 ms 3028 KB Output is correct
28 Correct 2 ms 3028 KB Output is correct
29 Correct 2 ms 3068 KB Output is correct
30 Correct 1 ms 3028 KB Output is correct
31 Correct 2 ms 3028 KB Output is correct
32 Correct 2 ms 3028 KB Output is correct
33 Correct 2 ms 3028 KB Output is correct
34 Correct 3 ms 3156 KB Output is correct
35 Correct 3 ms 3156 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 3 ms 3156 KB Output is correct
39 Correct 3 ms 3196 KB Output is correct
40 Correct 4 ms 3156 KB Output is correct
41 Correct 4 ms 3196 KB Output is correct
42 Correct 5 ms 3156 KB Output is correct
43 Correct 4 ms 3156 KB Output is correct
44 Correct 4 ms 3156 KB Output is correct
45 Correct 2 ms 3156 KB Output is correct
46 Correct 3 ms 3164 KB Output is correct
47 Correct 3 ms 2688 KB Output is correct
48 Correct 2 ms 2688 KB Output is correct
49 Correct 2 ms 2660 KB Output is correct
50 Correct 2 ms 2704 KB Output is correct
51 Correct 2 ms 2700 KB Output is correct
52 Correct 2 ms 2708 KB Output is correct
53 Correct 3 ms 3172 KB Output is correct
54 Correct 4 ms 3172 KB Output is correct
55 Correct 2 ms 2660 KB Output is correct
56 Correct 2 ms 2644 KB Output is correct
57 Correct 5 ms 3156 KB Output is correct
58 Correct 5 ms 3192 KB Output is correct
59 Correct 4 ms 3200 KB Output is correct
60 Correct 3 ms 3196 KB Output is correct
61 Correct 3 ms 3156 KB Output is correct
62 Correct 3 ms 3200 KB Output is correct
63 Correct 2 ms 3156 KB Output is correct
64 Correct 2 ms 3080 KB Output is correct
65 Correct 3 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 6100 KB Output is correct
2 Correct 6 ms 2868 KB Output is correct
3 Correct 17 ms 6112 KB Output is correct
4 Correct 5 ms 2836 KB Output is correct
5 Correct 19 ms 6084 KB Output is correct
6 Correct 19 ms 6112 KB Output is correct
7 Correct 19 ms 6092 KB Output is correct
8 Correct 17 ms 6000 KB Output is correct
9 Correct 17 ms 6044 KB Output is correct
10 Correct 18 ms 6052 KB Output is correct
11 Correct 37 ms 9168 KB Output is correct
12 Correct 34 ms 9036 KB Output is correct
13 Correct 36 ms 9024 KB Output is correct
14 Correct 37 ms 9036 KB Output is correct
15 Correct 8 ms 3028 KB Output is correct
16 Correct 8 ms 3028 KB Output is correct
17 Correct 8 ms 3028 KB Output is correct
18 Correct 8 ms 3028 KB Output is correct
19 Correct 8 ms 3028 KB Output is correct
20 Correct 8 ms 2984 KB Output is correct
21 Correct 8 ms 3064 KB Output is correct
22 Correct 9 ms 3028 KB Output is correct
23 Correct 8 ms 3040 KB Output is correct
24 Correct 32 ms 9112 KB Output is correct
25 Correct 36 ms 9036 KB Output is correct
26 Correct 36 ms 9036 KB Output is correct
27 Correct 33 ms 9068 KB Output is correct
28 Correct 33 ms 9036 KB Output is correct
29 Correct 36 ms 9080 KB Output is correct
30 Correct 13 ms 4820 KB Output is correct
31 Correct 18 ms 6028 KB Output is correct
32 Correct 40 ms 8484 KB Output is correct
33 Correct 35 ms 9036 KB Output is correct
34 Correct 5 ms 3668 KB Output is correct
35 Correct 7 ms 4180 KB Output is correct
36 Correct 17 ms 6100 KB Output is correct
37 Correct 31 ms 9044 KB Output is correct
38 Correct 34 ms 9120 KB Output is correct
39 Correct 38 ms 9120 KB Output is correct
40 Correct 31 ms 9072 KB Output is correct
41 Correct 35 ms 9028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 6100 KB Output is correct
2 Correct 6 ms 2868 KB Output is correct
3 Correct 17 ms 6112 KB Output is correct
4 Correct 5 ms 2836 KB Output is correct
5 Correct 19 ms 6084 KB Output is correct
6 Correct 19 ms 6112 KB Output is correct
7 Correct 19 ms 6092 KB Output is correct
8 Correct 17 ms 6000 KB Output is correct
9 Correct 17 ms 6044 KB Output is correct
10 Correct 18 ms 6052 KB Output is correct
11 Correct 37 ms 9168 KB Output is correct
12 Correct 34 ms 9036 KB Output is correct
13 Correct 36 ms 9024 KB Output is correct
14 Correct 37 ms 9036 KB Output is correct
15 Correct 8 ms 3028 KB Output is correct
16 Correct 8 ms 3028 KB Output is correct
17 Correct 8 ms 3028 KB Output is correct
18 Correct 8 ms 3028 KB Output is correct
19 Correct 8 ms 3028 KB Output is correct
20 Correct 8 ms 2984 KB Output is correct
21 Correct 8 ms 3064 KB Output is correct
22 Correct 9 ms 3028 KB Output is correct
23 Correct 8 ms 3040 KB Output is correct
24 Correct 32 ms 9112 KB Output is correct
25 Correct 36 ms 9036 KB Output is correct
26 Correct 36 ms 9036 KB Output is correct
27 Correct 33 ms 9068 KB Output is correct
28 Correct 33 ms 9036 KB Output is correct
29 Correct 36 ms 9080 KB Output is correct
30 Correct 13 ms 4820 KB Output is correct
31 Correct 18 ms 6028 KB Output is correct
32 Correct 40 ms 8484 KB Output is correct
33 Correct 35 ms 9036 KB Output is correct
34 Correct 5 ms 3668 KB Output is correct
35 Correct 7 ms 4180 KB Output is correct
36 Correct 17 ms 6100 KB Output is correct
37 Correct 31 ms 9044 KB Output is correct
38 Correct 34 ms 9120 KB Output is correct
39 Correct 38 ms 9120 KB Output is correct
40 Correct 31 ms 9072 KB Output is correct
41 Correct 35 ms 9028 KB Output is correct
42 Correct 21 ms 6100 KB Output is correct
43 Correct 7 ms 3080 KB Output is correct
44 Correct 22 ms 6124 KB Output is correct
45 Correct 6 ms 3156 KB Output is correct
46 Correct 6 ms 3028 KB Output is correct
47 Correct 5 ms 3056 KB Output is correct
48 Correct 6 ms 3028 KB Output is correct
49 Correct 22 ms 6100 KB Output is correct
50 Correct 23 ms 6124 KB Output is correct
51 Correct 28 ms 6128 KB Output is correct
52 Correct 45 ms 9316 KB Output is correct
53 Correct 42 ms 9288 KB Output is correct
54 Correct 11 ms 3540 KB Output is correct
55 Correct 10 ms 3588 KB Output is correct
56 Correct 38 ms 8808 KB Output is correct
57 Correct 43 ms 9376 KB Output is correct
58 Correct 48 ms 9384 KB Output is correct
59 Correct 43 ms 9240 KB Output is correct
60 Correct 40 ms 9432 KB Output is correct
61 Correct 36 ms 9304 KB Output is correct
62 Correct 28 ms 9084 KB Output is correct
63 Correct 43 ms 9544 KB Output is correct
64 Correct 41 ms 9564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 1 ms 3028 KB Output is correct
3 Correct 3 ms 3028 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2664 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 3028 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 2 ms 3064 KB Output is correct
15 Correct 3 ms 3028 KB Output is correct
16 Correct 2 ms 3028 KB Output is correct
17 Correct 2 ms 3028 KB Output is correct
18 Correct 2 ms 3028 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2680 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 3 ms 3068 KB Output is correct
26 Correct 2 ms 3028 KB Output is correct
27 Correct 2 ms 3028 KB Output is correct
28 Correct 2 ms 3028 KB Output is correct
29 Correct 2 ms 3068 KB Output is correct
30 Correct 1 ms 3028 KB Output is correct
31 Correct 2 ms 3028 KB Output is correct
32 Correct 2 ms 3028 KB Output is correct
33 Correct 2 ms 3028 KB Output is correct
34 Correct 3 ms 3156 KB Output is correct
35 Correct 3 ms 3156 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 3 ms 3156 KB Output is correct
39 Correct 3 ms 3196 KB Output is correct
40 Correct 4 ms 3156 KB Output is correct
41 Correct 4 ms 3196 KB Output is correct
42 Correct 5 ms 3156 KB Output is correct
43 Correct 4 ms 3156 KB Output is correct
44 Correct 4 ms 3156 KB Output is correct
45 Correct 2 ms 3156 KB Output is correct
46 Correct 3 ms 3164 KB Output is correct
47 Correct 3 ms 2688 KB Output is correct
48 Correct 2 ms 2688 KB Output is correct
49 Correct 2 ms 2660 KB Output is correct
50 Correct 2 ms 2704 KB Output is correct
51 Correct 2 ms 2700 KB Output is correct
52 Correct 2 ms 2708 KB Output is correct
53 Correct 3 ms 3172 KB Output is correct
54 Correct 4 ms 3172 KB Output is correct
55 Correct 2 ms 2660 KB Output is correct
56 Correct 2 ms 2644 KB Output is correct
57 Correct 5 ms 3156 KB Output is correct
58 Correct 5 ms 3192 KB Output is correct
59 Correct 4 ms 3200 KB Output is correct
60 Correct 3 ms 3196 KB Output is correct
61 Correct 3 ms 3156 KB Output is correct
62 Correct 3 ms 3200 KB Output is correct
63 Correct 2 ms 3156 KB Output is correct
64 Correct 2 ms 3080 KB Output is correct
65 Correct 3 ms 3156 KB Output is correct
66 Correct 17 ms 6100 KB Output is correct
67 Correct 6 ms 2868 KB Output is correct
68 Correct 17 ms 6112 KB Output is correct
69 Correct 5 ms 2836 KB Output is correct
70 Correct 19 ms 6084 KB Output is correct
71 Correct 19 ms 6112 KB Output is correct
72 Correct 19 ms 6092 KB Output is correct
73 Correct 17 ms 6000 KB Output is correct
74 Correct 17 ms 6044 KB Output is correct
75 Correct 18 ms 6052 KB Output is correct
76 Correct 37 ms 9168 KB Output is correct
77 Correct 34 ms 9036 KB Output is correct
78 Correct 36 ms 9024 KB Output is correct
79 Correct 37 ms 9036 KB Output is correct
80 Correct 8 ms 3028 KB Output is correct
81 Correct 8 ms 3028 KB Output is correct
82 Correct 8 ms 3028 KB Output is correct
83 Correct 8 ms 3028 KB Output is correct
84 Correct 8 ms 3028 KB Output is correct
85 Correct 8 ms 2984 KB Output is correct
86 Correct 8 ms 3064 KB Output is correct
87 Correct 9 ms 3028 KB Output is correct
88 Correct 8 ms 3040 KB Output is correct
89 Correct 32 ms 9112 KB Output is correct
90 Correct 36 ms 9036 KB Output is correct
91 Correct 36 ms 9036 KB Output is correct
92 Correct 33 ms 9068 KB Output is correct
93 Correct 33 ms 9036 KB Output is correct
94 Correct 36 ms 9080 KB Output is correct
95 Correct 13 ms 4820 KB Output is correct
96 Correct 18 ms 6028 KB Output is correct
97 Correct 40 ms 8484 KB Output is correct
98 Correct 35 ms 9036 KB Output is correct
99 Correct 5 ms 3668 KB Output is correct
100 Correct 7 ms 4180 KB Output is correct
101 Correct 17 ms 6100 KB Output is correct
102 Correct 31 ms 9044 KB Output is correct
103 Correct 34 ms 9120 KB Output is correct
104 Correct 38 ms 9120 KB Output is correct
105 Correct 31 ms 9072 KB Output is correct
106 Correct 35 ms 9028 KB Output is correct
107 Correct 21 ms 6100 KB Output is correct
108 Correct 7 ms 3080 KB Output is correct
109 Correct 22 ms 6124 KB Output is correct
110 Correct 6 ms 3156 KB Output is correct
111 Correct 6 ms 3028 KB Output is correct
112 Correct 5 ms 3056 KB Output is correct
113 Correct 6 ms 3028 KB Output is correct
114 Correct 22 ms 6100 KB Output is correct
115 Correct 23 ms 6124 KB Output is correct
116 Correct 28 ms 6128 KB Output is correct
117 Correct 45 ms 9316 KB Output is correct
118 Correct 42 ms 9288 KB Output is correct
119 Correct 11 ms 3540 KB Output is correct
120 Correct 10 ms 3588 KB Output is correct
121 Correct 38 ms 8808 KB Output is correct
122 Correct 43 ms 9376 KB Output is correct
123 Correct 48 ms 9384 KB Output is correct
124 Correct 43 ms 9240 KB Output is correct
125 Correct 40 ms 9432 KB Output is correct
126 Correct 36 ms 9304 KB Output is correct
127 Correct 28 ms 9084 KB Output is correct
128 Correct 43 ms 9544 KB Output is correct
129 Correct 41 ms 9564 KB Output is correct
130 Correct 2 ms 3068 KB Output is correct
131 Correct 2 ms 3028 KB Output is correct
132 Correct 2 ms 3028 KB Output is correct
133 Correct 87 ms 9232 KB Output is correct
134 Correct 109 ms 9244 KB Output is correct
135 Correct 116 ms 9300 KB Output is correct
136 Correct 9 ms 3512 KB Output is correct
137 Correct 9 ms 3508 KB Output is correct
138 Correct 9 ms 3540 KB Output is correct
139 Correct 10 ms 3540 KB Output is correct
140 Correct 10 ms 3540 KB Output is correct
141 Correct 13 ms 3512 KB Output is correct
142 Correct 122 ms 9384 KB Output is correct
143 Correct 116 ms 9248 KB Output is correct
144 Correct 113 ms 9420 KB Output is correct
145 Correct 97 ms 8748 KB Output is correct
146 Correct 109 ms 9332 KB Output is correct
147 Correct 120 ms 9292 KB Output is correct
148 Correct 84 ms 9280 KB Output is correct
149 Correct 145 ms 9288 KB Output is correct
150 Correct 60 ms 9288 KB Output is correct
151 Correct 81 ms 9536 KB Output is correct
152 Correct 91 ms 9536 KB Output is correct
153 Correct 36 ms 9512 KB Output is correct
154 Correct 37 ms 9592 KB Output is correct
155 Correct 38 ms 9596 KB Output is correct
156 Correct 35 ms 9544 KB Output is correct
157 Correct 34 ms 9656 KB Output is correct
158 Correct 34 ms 9592 KB Output is correct
159 Correct 33 ms 9604 KB Output is correct
160 Correct 35 ms 9544 KB Output is correct