Submission #949606

# Submission time Handle Problem Language Result Execution time Memory
949606 2024-03-19T12:08:49 Z KiaRez Watermelon (INOI20_watermelon) C++17
100 / 100
96 ms 37968 KB
/*
    IN THE NAME OF GOD
*/
#include <bits/stdc++.h>

// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;

#define F                                      first
#define S                                      second
#define Mp                                     make_pair
#define pb                                     push_back
#define pf                                     push_front
#define size(x)                                ((ll)x.size())
#define all(x)                                 (x).begin(),(x).end()
#define kill(x)		                           cout << x << '\n', exit(0);
#define fuck(x)                                cout << "(" << #x << " , " << x << ")" << endl
#define endl                                   '\n'

const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;

inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
    ll ans = 1;
    a=MOD(a, mod);
    while (b) {
        if (b & 1) ans = MOD(ans*a, mod);
        b >>= 1;
        a = MOD(a*a, mod);
    }
    return ans;
}

int n, k, b[N], deg[N], p[N], seg[2*N], r[N];
vector<int> neg, adj[N];
set <int> pq;

void upd(int ind, int val) {
	while(ind > 0) {
		seg[ind] = max(seg[ind], val);
		ind/=2;
	}
}

int qry(int l, int r, int ind=1, int lc=1, int rc=(1<<lg)+1) {
	if(l>=rc || lc>=r) return 0;
	if(lc>=l && rc<=r) return seg[ind];
	int mid = (lc+rc)/2;
	return max(qry(l, r, 2*ind, lc, mid), qry(l, r, 2*ind+1, mid, rc));
}

int cnt = 0;
void solve(int now) {
	if(now <= n && size(pq) == 0) kill(-1);
	if(now == n+1) {
		if(k == 1) {
			for(int i=1; i<=n; i++) cout<<p[i]<<' ';
			cout<<endl;
			exit(0);
		}
		k--;
		return;
	}
	vector<int> tmp;
	for(auto it : pq) {
		if(size(tmp)==10) break;
		tmp.pb(it);
	}
	for(auto it : tmp) {
		for(int u : adj[it]) {
			deg[u] --;
			if(deg[u] == 0) pq.insert(u);
		}
		pq.erase(it);
		p[it] = now;

		solve(now+1);
		
		for(int u : adj[it]) {
			deg[u] ++;
			if(deg[u] == 1) pq.erase(u);
		}
		pq.insert(it);
	}
}

int main () {
	ios_base::sync_with_stdio(false), cin.tie(0);

	cin>>n>>k;
	for(int i=1; i<=n; i++) {
		cin>>b[i];
		upd(i+(1<<lg)-1, b[i]);
		if(b[i] == -1) neg.pb(i);
	}
	if(size(neg) == 0 || neg.back() < n) kill(-1);

	for(int i=0; i<size(neg); i++) {
		int ptr = (i==0 ? 1 : neg[i-1]+1);
		if(i > 0) adj[neg[i]].pb(neg[i-1]);
		
		for(int j=ptr; j<neg[i]; j++) adj[j].pb(neg[i]);

		for(int j=neg[i]-1; j>=ptr; j--) {
			r[j] = j+1;
			while(r[j] < neg[i] && b[r[j]] < b[j]) {
				if(qry(j+1,r[j]) < b[r[j]]) adj[r[j]].pb(j);
				r[j] = r[r[j]];
			}
			adj[j].pb(r[j]);
			if(qry(j+1, r[j]) + 1 != b[j]) kill(-1);
		}
	}

	for(int i=1; i<=n; i++) for(auto u : adj[i]) deg[u] ++;
	for(int i=1; i<=n; i++) if(deg[i] == 0) pq.insert(i);

	solve(1);

	cout<<-1<<endl;

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 2 ms 8840 KB Output is correct
7 Correct 2 ms 9048 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8844 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 3 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 2 ms 8796 KB Output is correct
21 Correct 2 ms 8628 KB Output is correct
22 Correct 2 ms 8796 KB Output is correct
23 Correct 2 ms 8796 KB Output is correct
24 Correct 2 ms 8796 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10844 KB Output is correct
30 Correct 2 ms 10844 KB Output is correct
31 Correct 2 ms 10840 KB Output is correct
32 Correct 2 ms 10844 KB Output is correct
33 Correct 2 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 2 ms 8840 KB Output is correct
7 Correct 2 ms 9048 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8844 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 3 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 2 ms 8796 KB Output is correct
21 Correct 2 ms 8628 KB Output is correct
22 Correct 2 ms 8796 KB Output is correct
23 Correct 2 ms 8796 KB Output is correct
24 Correct 2 ms 8796 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10844 KB Output is correct
30 Correct 2 ms 10844 KB Output is correct
31 Correct 2 ms 10840 KB Output is correct
32 Correct 2 ms 10844 KB Output is correct
33 Correct 2 ms 10844 KB Output is correct
34 Correct 4 ms 11356 KB Output is correct
35 Correct 5 ms 11356 KB Output is correct
36 Correct 2 ms 8796 KB Output is correct
37 Correct 2 ms 8792 KB Output is correct
38 Correct 4 ms 11356 KB Output is correct
39 Correct 4 ms 11356 KB Output is correct
40 Correct 5 ms 11356 KB Output is correct
41 Correct 4 ms 11356 KB Output is correct
42 Correct 4 ms 11420 KB Output is correct
43 Correct 4 ms 11356 KB Output is correct
44 Correct 5 ms 11352 KB Output is correct
45 Correct 3 ms 11100 KB Output is correct
46 Correct 5 ms 11356 KB Output is correct
47 Correct 2 ms 8792 KB Output is correct
48 Correct 2 ms 8796 KB Output is correct
49 Correct 2 ms 8816 KB Output is correct
50 Correct 2 ms 8796 KB Output is correct
51 Correct 2 ms 8796 KB Output is correct
52 Correct 3 ms 8840 KB Output is correct
53 Correct 4 ms 11356 KB Output is correct
54 Correct 4 ms 11352 KB Output is correct
55 Correct 2 ms 8792 KB Output is correct
56 Correct 2 ms 8796 KB Output is correct
57 Correct 4 ms 11356 KB Output is correct
58 Correct 4 ms 11352 KB Output is correct
59 Correct 4 ms 11356 KB Output is correct
60 Correct 4 ms 11356 KB Output is correct
61 Correct 4 ms 11356 KB Output is correct
62 Correct 4 ms 11608 KB Output is correct
63 Correct 4 ms 11408 KB Output is correct
64 Correct 3 ms 11096 KB Output is correct
65 Correct 3 ms 11096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 24148 KB Output is correct
2 Correct 6 ms 9564 KB Output is correct
3 Correct 44 ms 24148 KB Output is correct
4 Correct 7 ms 10872 KB Output is correct
5 Correct 43 ms 24120 KB Output is correct
6 Correct 44 ms 24148 KB Output is correct
7 Correct 43 ms 24144 KB Output is correct
8 Correct 44 ms 24172 KB Output is correct
9 Correct 50 ms 24404 KB Output is correct
10 Correct 44 ms 24148 KB Output is correct
11 Correct 85 ms 37716 KB Output is correct
12 Correct 85 ms 37764 KB Output is correct
13 Correct 84 ms 37720 KB Output is correct
14 Correct 85 ms 37612 KB Output is correct
15 Correct 11 ms 11868 KB Output is correct
16 Correct 11 ms 11356 KB Output is correct
17 Correct 13 ms 12892 KB Output is correct
18 Correct 13 ms 13204 KB Output is correct
19 Correct 12 ms 12636 KB Output is correct
20 Correct 11 ms 11864 KB Output is correct
21 Correct 9 ms 10588 KB Output is correct
22 Correct 10 ms 10544 KB Output is correct
23 Correct 11 ms 11612 KB Output is correct
24 Correct 79 ms 34660 KB Output is correct
25 Correct 75 ms 34980 KB Output is correct
26 Correct 78 ms 35664 KB Output is correct
27 Correct 82 ms 36048 KB Output is correct
28 Correct 79 ms 35840 KB Output is correct
29 Correct 76 ms 34640 KB Output is correct
30 Correct 26 ms 18864 KB Output is correct
31 Correct 51 ms 24412 KB Output is correct
32 Correct 81 ms 35108 KB Output is correct
33 Correct 80 ms 37964 KB Output is correct
34 Correct 9 ms 13148 KB Output is correct
35 Correct 17 ms 15964 KB Output is correct
36 Correct 41 ms 24188 KB Output is correct
37 Correct 78 ms 37716 KB Output is correct
38 Correct 77 ms 37200 KB Output is correct
39 Correct 90 ms 37712 KB Output is correct
40 Correct 54 ms 35544 KB Output is correct
41 Correct 87 ms 33408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 24148 KB Output is correct
2 Correct 6 ms 9564 KB Output is correct
3 Correct 44 ms 24148 KB Output is correct
4 Correct 7 ms 10872 KB Output is correct
5 Correct 43 ms 24120 KB Output is correct
6 Correct 44 ms 24148 KB Output is correct
7 Correct 43 ms 24144 KB Output is correct
8 Correct 44 ms 24172 KB Output is correct
9 Correct 50 ms 24404 KB Output is correct
10 Correct 44 ms 24148 KB Output is correct
11 Correct 85 ms 37716 KB Output is correct
12 Correct 85 ms 37764 KB Output is correct
13 Correct 84 ms 37720 KB Output is correct
14 Correct 85 ms 37612 KB Output is correct
15 Correct 11 ms 11868 KB Output is correct
16 Correct 11 ms 11356 KB Output is correct
17 Correct 13 ms 12892 KB Output is correct
18 Correct 13 ms 13204 KB Output is correct
19 Correct 12 ms 12636 KB Output is correct
20 Correct 11 ms 11864 KB Output is correct
21 Correct 9 ms 10588 KB Output is correct
22 Correct 10 ms 10544 KB Output is correct
23 Correct 11 ms 11612 KB Output is correct
24 Correct 79 ms 34660 KB Output is correct
25 Correct 75 ms 34980 KB Output is correct
26 Correct 78 ms 35664 KB Output is correct
27 Correct 82 ms 36048 KB Output is correct
28 Correct 79 ms 35840 KB Output is correct
29 Correct 76 ms 34640 KB Output is correct
30 Correct 26 ms 18864 KB Output is correct
31 Correct 51 ms 24412 KB Output is correct
32 Correct 81 ms 35108 KB Output is correct
33 Correct 80 ms 37964 KB Output is correct
34 Correct 9 ms 13148 KB Output is correct
35 Correct 17 ms 15964 KB Output is correct
36 Correct 41 ms 24188 KB Output is correct
37 Correct 78 ms 37716 KB Output is correct
38 Correct 77 ms 37200 KB Output is correct
39 Correct 90 ms 37712 KB Output is correct
40 Correct 54 ms 35544 KB Output is correct
41 Correct 87 ms 33408 KB Output is correct
42 Correct 51 ms 24304 KB Output is correct
43 Correct 6 ms 10076 KB Output is correct
44 Correct 45 ms 24188 KB Output is correct
45 Correct 8 ms 10844 KB Output is correct
46 Correct 6 ms 9812 KB Output is correct
47 Correct 7 ms 10588 KB Output is correct
48 Correct 7 ms 10844 KB Output is correct
49 Correct 43 ms 24344 KB Output is correct
50 Correct 46 ms 24304 KB Output is correct
51 Correct 49 ms 24208 KB Output is correct
52 Correct 86 ms 37632 KB Output is correct
53 Correct 84 ms 37712 KB Output is correct
54 Correct 11 ms 11612 KB Output is correct
55 Correct 10 ms 11100 KB Output is correct
56 Correct 70 ms 35296 KB Output is correct
57 Correct 78 ms 37712 KB Output is correct
58 Correct 78 ms 37712 KB Output is correct
59 Correct 80 ms 37200 KB Output is correct
60 Correct 78 ms 37708 KB Output is correct
61 Correct 58 ms 35540 KB Output is correct
62 Correct 72 ms 32852 KB Output is correct
63 Correct 77 ms 34756 KB Output is correct
64 Correct 86 ms 34256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 2 ms 8840 KB Output is correct
7 Correct 2 ms 9048 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8844 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 3 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 2 ms 8796 KB Output is correct
21 Correct 2 ms 8628 KB Output is correct
22 Correct 2 ms 8796 KB Output is correct
23 Correct 2 ms 8796 KB Output is correct
24 Correct 2 ms 8796 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10844 KB Output is correct
30 Correct 2 ms 10844 KB Output is correct
31 Correct 2 ms 10840 KB Output is correct
32 Correct 2 ms 10844 KB Output is correct
33 Correct 2 ms 10844 KB Output is correct
34 Correct 4 ms 11356 KB Output is correct
35 Correct 5 ms 11356 KB Output is correct
36 Correct 2 ms 8796 KB Output is correct
37 Correct 2 ms 8792 KB Output is correct
38 Correct 4 ms 11356 KB Output is correct
39 Correct 4 ms 11356 KB Output is correct
40 Correct 5 ms 11356 KB Output is correct
41 Correct 4 ms 11356 KB Output is correct
42 Correct 4 ms 11420 KB Output is correct
43 Correct 4 ms 11356 KB Output is correct
44 Correct 5 ms 11352 KB Output is correct
45 Correct 3 ms 11100 KB Output is correct
46 Correct 5 ms 11356 KB Output is correct
47 Correct 2 ms 8792 KB Output is correct
48 Correct 2 ms 8796 KB Output is correct
49 Correct 2 ms 8816 KB Output is correct
50 Correct 2 ms 8796 KB Output is correct
51 Correct 2 ms 8796 KB Output is correct
52 Correct 3 ms 8840 KB Output is correct
53 Correct 4 ms 11356 KB Output is correct
54 Correct 4 ms 11352 KB Output is correct
55 Correct 2 ms 8792 KB Output is correct
56 Correct 2 ms 8796 KB Output is correct
57 Correct 4 ms 11356 KB Output is correct
58 Correct 4 ms 11352 KB Output is correct
59 Correct 4 ms 11356 KB Output is correct
60 Correct 4 ms 11356 KB Output is correct
61 Correct 4 ms 11356 KB Output is correct
62 Correct 4 ms 11608 KB Output is correct
63 Correct 4 ms 11408 KB Output is correct
64 Correct 3 ms 11096 KB Output is correct
65 Correct 3 ms 11096 KB Output is correct
66 Correct 43 ms 24148 KB Output is correct
67 Correct 6 ms 9564 KB Output is correct
68 Correct 44 ms 24148 KB Output is correct
69 Correct 7 ms 10872 KB Output is correct
70 Correct 43 ms 24120 KB Output is correct
71 Correct 44 ms 24148 KB Output is correct
72 Correct 43 ms 24144 KB Output is correct
73 Correct 44 ms 24172 KB Output is correct
74 Correct 50 ms 24404 KB Output is correct
75 Correct 44 ms 24148 KB Output is correct
76 Correct 85 ms 37716 KB Output is correct
77 Correct 85 ms 37764 KB Output is correct
78 Correct 84 ms 37720 KB Output is correct
79 Correct 85 ms 37612 KB Output is correct
80 Correct 11 ms 11868 KB Output is correct
81 Correct 11 ms 11356 KB Output is correct
82 Correct 13 ms 12892 KB Output is correct
83 Correct 13 ms 13204 KB Output is correct
84 Correct 12 ms 12636 KB Output is correct
85 Correct 11 ms 11864 KB Output is correct
86 Correct 9 ms 10588 KB Output is correct
87 Correct 10 ms 10544 KB Output is correct
88 Correct 11 ms 11612 KB Output is correct
89 Correct 79 ms 34660 KB Output is correct
90 Correct 75 ms 34980 KB Output is correct
91 Correct 78 ms 35664 KB Output is correct
92 Correct 82 ms 36048 KB Output is correct
93 Correct 79 ms 35840 KB Output is correct
94 Correct 76 ms 34640 KB Output is correct
95 Correct 26 ms 18864 KB Output is correct
96 Correct 51 ms 24412 KB Output is correct
97 Correct 81 ms 35108 KB Output is correct
98 Correct 80 ms 37964 KB Output is correct
99 Correct 9 ms 13148 KB Output is correct
100 Correct 17 ms 15964 KB Output is correct
101 Correct 41 ms 24188 KB Output is correct
102 Correct 78 ms 37716 KB Output is correct
103 Correct 77 ms 37200 KB Output is correct
104 Correct 90 ms 37712 KB Output is correct
105 Correct 54 ms 35544 KB Output is correct
106 Correct 87 ms 33408 KB Output is correct
107 Correct 51 ms 24304 KB Output is correct
108 Correct 6 ms 10076 KB Output is correct
109 Correct 45 ms 24188 KB Output is correct
110 Correct 8 ms 10844 KB Output is correct
111 Correct 6 ms 9812 KB Output is correct
112 Correct 7 ms 10588 KB Output is correct
113 Correct 7 ms 10844 KB Output is correct
114 Correct 43 ms 24344 KB Output is correct
115 Correct 46 ms 24304 KB Output is correct
116 Correct 49 ms 24208 KB Output is correct
117 Correct 86 ms 37632 KB Output is correct
118 Correct 84 ms 37712 KB Output is correct
119 Correct 11 ms 11612 KB Output is correct
120 Correct 10 ms 11100 KB Output is correct
121 Correct 70 ms 35296 KB Output is correct
122 Correct 78 ms 37712 KB Output is correct
123 Correct 78 ms 37712 KB Output is correct
124 Correct 80 ms 37200 KB Output is correct
125 Correct 78 ms 37708 KB Output is correct
126 Correct 58 ms 35540 KB Output is correct
127 Correct 72 ms 32852 KB Output is correct
128 Correct 77 ms 34756 KB Output is correct
129 Correct 86 ms 34256 KB Output is correct
130 Correct 2 ms 10844 KB Output is correct
131 Correct 2 ms 10844 KB Output is correct
132 Correct 2 ms 10844 KB Output is correct
133 Correct 85 ms 37796 KB Output is correct
134 Correct 86 ms 37716 KB Output is correct
135 Correct 88 ms 37968 KB Output is correct
136 Correct 13 ms 13148 KB Output is correct
137 Correct 13 ms 12636 KB Output is correct
138 Correct 11 ms 12124 KB Output is correct
139 Correct 11 ms 11356 KB Output is correct
140 Correct 12 ms 12892 KB Output is correct
141 Correct 11 ms 10844 KB Output is correct
142 Correct 87 ms 37716 KB Output is correct
143 Correct 90 ms 37860 KB Output is correct
144 Correct 78 ms 37740 KB Output is correct
145 Correct 71 ms 35100 KB Output is correct
146 Correct 79 ms 37708 KB Output is correct
147 Correct 88 ms 37344 KB Output is correct
148 Correct 85 ms 35540 KB Output is correct
149 Correct 96 ms 35644 KB Output is correct
150 Correct 71 ms 35540 KB Output is correct
151 Correct 80 ms 34900 KB Output is correct
152 Correct 86 ms 36048 KB Output is correct
153 Correct 80 ms 35920 KB Output is correct
154 Correct 80 ms 34616 KB Output is correct
155 Correct 80 ms 35668 KB Output is correct
156 Correct 75 ms 33876 KB Output is correct
157 Correct 74 ms 34268 KB Output is correct
158 Correct 79 ms 35400 KB Output is correct
159 Correct 76 ms 35328 KB Output is correct
160 Correct 75 ms 35396 KB Output is correct