Submission #614126

# Submission time Handle Problem Language Result Execution time Memory
614126 2022-07-30T19:23:54 Z valerikk Rope (JOI17_rope) C++17
100 / 100
1959 ms 86320 KB
#include <bits/stdc++.h>

using namespace std;

struct tree {
	vector<int> t;
	vector<int> a;
	int n;

	void turnon(int i) {
		t[i + n] = a[i];
		for (i += n; i > 1; i >>= 1) {
			t[i >> 1] = max(t[i], t[i ^ 1]);
		}
	}

	void turnoff(int i) {
		t[i + n] = 0;
		for (i += n; i > 1; i >>= 1) {
			t[i >> 1] = max(t[i], t[i ^ 1]);
		}
	}

	int get_max() {
		return t[1];
	}

	tree(const vector<int> &a_) {
		a = a_;
		n = 1;
		while (n < (int) a.size()) {
			n <<= 1;
		}
		t.resize(2 * n, 0);
		a.resize(n, 0);
		for (int i = 0; i < n; ++i) {
			t[i + n] = a[i];
		}
		for (int i = n - 1; i > 0; --i) {
			t[i] = max(t[i << 1], t[i << 1 | 1]);
		}
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	vector<int> c(n);
	for (int i = 0; i < n; ++i) {
		cin >> c[i];
		--c[i];
	}
	vector<int> cnt(m, 0);
	for (int i = 0; i < n; ++i) {
		++cnt[c[i]];
	}
	vector<int> ans(m);
	for (int u = 0; u < m; ++u) {
		ans[u] = n - cnt[u];
	}
	tree t(cnt);
	for (int p = 0; p < 2; ++p) {
		vector<vector<int>> g(m);
		for (int i = p; i + 1 < n; i += 2) {
			if (c[i] != c[i + 1]) {
				g[c[i]].push_back(c[i + 1]);
				g[c[i + 1]].push_back(c[i]);
			}
		}
		for (int u = 0; u < m; ++u) {
			sort(begin(g[u]), end(g[u]));
			vector<int> tmp;
			int len = 0;
			for (int i = 0; i < (int) g[u].size(); ++i) {
				int v = g[u][i];
				++len;
				if (i == (int) g[u].size() - 1 || g[u][i + 1] != v) {
					ans[u] = min(ans[u], n - cnt[u] - cnt[v] + len);
					len = 0;
					tmp.push_back(v);
				}
			}
			g[u] = tmp;
			for (int v : g[u]) {
				t.turnoff(v);
			}
			t.turnoff(u);
			ans[u] = min(ans[u], n - cnt[u] - t.get_max());
			t.turnon(u);
			for (int v : g[u]) {
				t.turnon(v);
			}
		}
	}
	for (int u = 0; u < m; ++u) {
		cout << ans[u] << "\n";
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 15 ms 1364 KB Output is correct
28 Correct 13 ms 1240 KB Output is correct
29 Correct 13 ms 1236 KB Output is correct
30 Correct 10 ms 1108 KB Output is correct
31 Correct 12 ms 1284 KB Output is correct
32 Correct 10 ms 1236 KB Output is correct
33 Correct 12 ms 1236 KB Output is correct
34 Correct 10 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 15 ms 1364 KB Output is correct
28 Correct 13 ms 1240 KB Output is correct
29 Correct 13 ms 1236 KB Output is correct
30 Correct 10 ms 1108 KB Output is correct
31 Correct 12 ms 1284 KB Output is correct
32 Correct 10 ms 1236 KB Output is correct
33 Correct 12 ms 1236 KB Output is correct
34 Correct 10 ms 1112 KB Output is correct
35 Correct 22 ms 1320 KB Output is correct
36 Correct 21 ms 1236 KB Output is correct
37 Correct 26 ms 1312 KB Output is correct
38 Correct 24 ms 1324 KB Output is correct
39 Correct 22 ms 1236 KB Output is correct
40 Correct 22 ms 1360 KB Output is correct
41 Correct 20 ms 1416 KB Output is correct
42 Correct 19 ms 1364 KB Output is correct
43 Correct 18 ms 1364 KB Output is correct
44 Correct 19 ms 1400 KB Output is correct
45 Correct 18 ms 1364 KB Output is correct
46 Correct 18 ms 1364 KB Output is correct
47 Correct 18 ms 1412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 15 ms 1364 KB Output is correct
28 Correct 13 ms 1240 KB Output is correct
29 Correct 13 ms 1236 KB Output is correct
30 Correct 10 ms 1108 KB Output is correct
31 Correct 12 ms 1284 KB Output is correct
32 Correct 10 ms 1236 KB Output is correct
33 Correct 12 ms 1236 KB Output is correct
34 Correct 10 ms 1112 KB Output is correct
35 Correct 22 ms 1320 KB Output is correct
36 Correct 21 ms 1236 KB Output is correct
37 Correct 26 ms 1312 KB Output is correct
38 Correct 24 ms 1324 KB Output is correct
39 Correct 22 ms 1236 KB Output is correct
40 Correct 22 ms 1360 KB Output is correct
41 Correct 20 ms 1416 KB Output is correct
42 Correct 19 ms 1364 KB Output is correct
43 Correct 18 ms 1364 KB Output is correct
44 Correct 19 ms 1400 KB Output is correct
45 Correct 18 ms 1364 KB Output is correct
46 Correct 18 ms 1364 KB Output is correct
47 Correct 18 ms 1412 KB Output is correct
48 Correct 245 ms 11036 KB Output is correct
49 Correct 241 ms 11076 KB Output is correct
50 Correct 246 ms 11064 KB Output is correct
51 Correct 242 ms 10948 KB Output is correct
52 Correct 218 ms 10000 KB Output is correct
53 Correct 213 ms 10228 KB Output is correct
54 Correct 200 ms 9360 KB Output is correct
55 Correct 231 ms 9056 KB Output is correct
56 Correct 195 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 15 ms 1364 KB Output is correct
28 Correct 13 ms 1240 KB Output is correct
29 Correct 13 ms 1236 KB Output is correct
30 Correct 10 ms 1108 KB Output is correct
31 Correct 12 ms 1284 KB Output is correct
32 Correct 10 ms 1236 KB Output is correct
33 Correct 12 ms 1236 KB Output is correct
34 Correct 10 ms 1112 KB Output is correct
35 Correct 22 ms 1320 KB Output is correct
36 Correct 21 ms 1236 KB Output is correct
37 Correct 26 ms 1312 KB Output is correct
38 Correct 24 ms 1324 KB Output is correct
39 Correct 22 ms 1236 KB Output is correct
40 Correct 22 ms 1360 KB Output is correct
41 Correct 20 ms 1416 KB Output is correct
42 Correct 19 ms 1364 KB Output is correct
43 Correct 18 ms 1364 KB Output is correct
44 Correct 19 ms 1400 KB Output is correct
45 Correct 18 ms 1364 KB Output is correct
46 Correct 18 ms 1364 KB Output is correct
47 Correct 18 ms 1412 KB Output is correct
48 Correct 245 ms 11036 KB Output is correct
49 Correct 241 ms 11076 KB Output is correct
50 Correct 246 ms 11064 KB Output is correct
51 Correct 242 ms 10948 KB Output is correct
52 Correct 218 ms 10000 KB Output is correct
53 Correct 213 ms 10228 KB Output is correct
54 Correct 200 ms 9360 KB Output is correct
55 Correct 231 ms 9056 KB Output is correct
56 Correct 195 ms 8796 KB Output is correct
57 Correct 1959 ms 86320 KB Output is correct
58 Correct 1387 ms 64972 KB Output is correct
59 Correct 1561 ms 64820 KB Output is correct
60 Correct 1672 ms 71904 KB Output is correct
61 Correct 1754 ms 71908 KB Output is correct
62 Correct 586 ms 40792 KB Output is correct
63 Correct 867 ms 48876 KB Output is correct
64 Correct 691 ms 43872 KB Output is correct
65 Correct 355 ms 28016 KB Output is correct