Submission #203232

# Submission time Handle Problem Language Result Execution time Memory
203232 2020-02-19T21:13:48 Z Noam527 Rope (JOI17_rope) C++17
100 / 100
1174 ms 76920 KB
#include <bits/stdc++.h>
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll inf = 4e18;
const int OO = 0;
const int OOO = 1;
using namespace std;

const int N = 1e6 + 10;

int n, m, a[N];
vector<int> pos[N];

int mx;
int freq[N] = {};
int freq_count[N] = {};

void add(int val) {
	if (OO) cout << "adding " << val << '\n';
	--freq_count[freq[val]];
	++freq[val];
	++freq_count[freq[val]];
	mx = max(mx, freq[val]);
}
void del(int val) {
	if (OO) cout << "deleting " << val << '\n';
	--freq_count[freq[val]];
	if (mx == freq[val] && !freq_count[freq[val]])
		--mx;
	--freq[val];
	++freq_count[freq[val]];
}
int query() {
	if (OO) cout << "query: " << mx << '\n';
	return mx;
}
void prep() {
	freq_count[0] = n;
	mx = 0;
	for (int i = 0; i < n; i++)
		add(a[i]);
}

int solve(int v) {
	if (OO) cout << "started solve(" << v << "):\n";
	vector<int> even, odd;
	for (const auto &i : pos[v]) {
		int e = (i & 1) ? i - 1 : i;
		int o = (i & 1) ? i : i - 1;
		if (!even.size() || even.back() != e) even.push_back(e);
		if (o > 0 && (!odd.size() || odd.back() != o)) odd.push_back(o);
	}

	int start, remain, mn = md;

	// try even
	start = 0;
	remain = n;
	for (const auto &i : even) {
		del(a[i]);
		remain--;
		if (a[i] != v)
			start++;
		if (i + 1 < n) {
			del(a[i + 1]);
			remain--;
			if (a[i + 1] != v)
				start++;
		}
	}
	mn = min(mn, start + remain - query());
	for (const auto &i : even) {
		add(a[i]);
		if (i + 1 < n)
			add(a[i + 1]);
	}
	// try odd
	start = 0;
	remain = n;
	if (a[0] == v) {
		del(a[0]);
		remain--;
	}
	for (const auto &i : odd) {
		del(a[i]);
		remain--;
		if (a[i] != v)
			start++;
		if (i + 1 < n) {
			del(a[i + 1]);
			remain--;
			if (a[i + 1] != v)
				start++;
		}
	}
	mn = min(mn, start + remain - query());
	if (a[0] == v)
		add(a[0]);
	for (const auto &i : odd) {
		add(a[i]);
		if (i + 1 < n)
			add(a[i + 1]);
	}

	return mn;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		--a[i];
		pos[a[i]].push_back(i);
	}
	prep();
	for (int i = 0; i < m; i++)
		cout << solve(i) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23800 KB Output is correct
2 Correct 20 ms 23800 KB Output is correct
3 Correct 21 ms 23932 KB Output is correct
4 Correct 20 ms 23800 KB Output is correct
5 Correct 20 ms 23928 KB Output is correct
6 Correct 20 ms 23800 KB Output is correct
7 Correct 21 ms 23800 KB Output is correct
8 Correct 20 ms 23928 KB Output is correct
9 Correct 20 ms 23800 KB Output is correct
10 Correct 19 ms 23928 KB Output is correct
11 Correct 21 ms 23928 KB Output is correct
12 Correct 20 ms 23800 KB Output is correct
13 Correct 20 ms 23800 KB Output is correct
14 Correct 20 ms 23800 KB Output is correct
15 Correct 20 ms 23800 KB Output is correct
16 Correct 20 ms 23800 KB Output is correct
17 Correct 20 ms 23800 KB Output is correct
18 Correct 20 ms 23800 KB Output is correct
19 Correct 20 ms 23800 KB Output is correct
20 Correct 20 ms 23928 KB Output is correct
21 Correct 19 ms 23800 KB Output is correct
22 Correct 20 ms 23800 KB Output is correct
23 Correct 20 ms 23800 KB Output is correct
24 Correct 21 ms 23928 KB Output is correct
25 Correct 23 ms 23804 KB Output is correct
26 Correct 20 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23800 KB Output is correct
2 Correct 20 ms 23800 KB Output is correct
3 Correct 21 ms 23932 KB Output is correct
4 Correct 20 ms 23800 KB Output is correct
5 Correct 20 ms 23928 KB Output is correct
6 Correct 20 ms 23800 KB Output is correct
7 Correct 21 ms 23800 KB Output is correct
8 Correct 20 ms 23928 KB Output is correct
9 Correct 20 ms 23800 KB Output is correct
10 Correct 19 ms 23928 KB Output is correct
11 Correct 21 ms 23928 KB Output is correct
12 Correct 20 ms 23800 KB Output is correct
13 Correct 20 ms 23800 KB Output is correct
14 Correct 20 ms 23800 KB Output is correct
15 Correct 20 ms 23800 KB Output is correct
16 Correct 20 ms 23800 KB Output is correct
17 Correct 20 ms 23800 KB Output is correct
18 Correct 20 ms 23800 KB Output is correct
19 Correct 20 ms 23800 KB Output is correct
20 Correct 20 ms 23928 KB Output is correct
21 Correct 19 ms 23800 KB Output is correct
22 Correct 20 ms 23800 KB Output is correct
23 Correct 20 ms 23800 KB Output is correct
24 Correct 21 ms 23928 KB Output is correct
25 Correct 23 ms 23804 KB Output is correct
26 Correct 20 ms 23800 KB Output is correct
27 Correct 37 ms 25208 KB Output is correct
28 Correct 35 ms 25448 KB Output is correct
29 Correct 40 ms 25276 KB Output is correct
30 Correct 35 ms 25396 KB Output is correct
31 Correct 36 ms 25208 KB Output is correct
32 Correct 36 ms 25404 KB Output is correct
33 Correct 36 ms 25208 KB Output is correct
34 Correct 37 ms 25464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23800 KB Output is correct
2 Correct 20 ms 23800 KB Output is correct
3 Correct 21 ms 23932 KB Output is correct
4 Correct 20 ms 23800 KB Output is correct
5 Correct 20 ms 23928 KB Output is correct
6 Correct 20 ms 23800 KB Output is correct
7 Correct 21 ms 23800 KB Output is correct
8 Correct 20 ms 23928 KB Output is correct
9 Correct 20 ms 23800 KB Output is correct
10 Correct 19 ms 23928 KB Output is correct
11 Correct 21 ms 23928 KB Output is correct
12 Correct 20 ms 23800 KB Output is correct
13 Correct 20 ms 23800 KB Output is correct
14 Correct 20 ms 23800 KB Output is correct
15 Correct 20 ms 23800 KB Output is correct
16 Correct 20 ms 23800 KB Output is correct
17 Correct 20 ms 23800 KB Output is correct
18 Correct 20 ms 23800 KB Output is correct
19 Correct 20 ms 23800 KB Output is correct
20 Correct 20 ms 23928 KB Output is correct
21 Correct 19 ms 23800 KB Output is correct
22 Correct 20 ms 23800 KB Output is correct
23 Correct 20 ms 23800 KB Output is correct
24 Correct 21 ms 23928 KB Output is correct
25 Correct 23 ms 23804 KB Output is correct
26 Correct 20 ms 23800 KB Output is correct
27 Correct 37 ms 25208 KB Output is correct
28 Correct 35 ms 25448 KB Output is correct
29 Correct 40 ms 25276 KB Output is correct
30 Correct 35 ms 25396 KB Output is correct
31 Correct 36 ms 25208 KB Output is correct
32 Correct 36 ms 25404 KB Output is correct
33 Correct 36 ms 25208 KB Output is correct
34 Correct 37 ms 25464 KB Output is correct
35 Correct 42 ms 25208 KB Output is correct
36 Correct 40 ms 25208 KB Output is correct
37 Correct 46 ms 25208 KB Output is correct
38 Correct 41 ms 25208 KB Output is correct
39 Correct 74 ms 25208 KB Output is correct
40 Correct 41 ms 25336 KB Output is correct
41 Correct 40 ms 25208 KB Output is correct
42 Correct 40 ms 25208 KB Output is correct
43 Correct 39 ms 25208 KB Output is correct
44 Correct 39 ms 25208 KB Output is correct
45 Correct 41 ms 25336 KB Output is correct
46 Correct 41 ms 25208 KB Output is correct
47 Correct 40 ms 25208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23800 KB Output is correct
2 Correct 20 ms 23800 KB Output is correct
3 Correct 21 ms 23932 KB Output is correct
4 Correct 20 ms 23800 KB Output is correct
5 Correct 20 ms 23928 KB Output is correct
6 Correct 20 ms 23800 KB Output is correct
7 Correct 21 ms 23800 KB Output is correct
8 Correct 20 ms 23928 KB Output is correct
9 Correct 20 ms 23800 KB Output is correct
10 Correct 19 ms 23928 KB Output is correct
11 Correct 21 ms 23928 KB Output is correct
12 Correct 20 ms 23800 KB Output is correct
13 Correct 20 ms 23800 KB Output is correct
14 Correct 20 ms 23800 KB Output is correct
15 Correct 20 ms 23800 KB Output is correct
16 Correct 20 ms 23800 KB Output is correct
17 Correct 20 ms 23800 KB Output is correct
18 Correct 20 ms 23800 KB Output is correct
19 Correct 20 ms 23800 KB Output is correct
20 Correct 20 ms 23928 KB Output is correct
21 Correct 19 ms 23800 KB Output is correct
22 Correct 20 ms 23800 KB Output is correct
23 Correct 20 ms 23800 KB Output is correct
24 Correct 21 ms 23928 KB Output is correct
25 Correct 23 ms 23804 KB Output is correct
26 Correct 20 ms 23800 KB Output is correct
27 Correct 37 ms 25208 KB Output is correct
28 Correct 35 ms 25448 KB Output is correct
29 Correct 40 ms 25276 KB Output is correct
30 Correct 35 ms 25396 KB Output is correct
31 Correct 36 ms 25208 KB Output is correct
32 Correct 36 ms 25404 KB Output is correct
33 Correct 36 ms 25208 KB Output is correct
34 Correct 37 ms 25464 KB Output is correct
35 Correct 42 ms 25208 KB Output is correct
36 Correct 40 ms 25208 KB Output is correct
37 Correct 46 ms 25208 KB Output is correct
38 Correct 41 ms 25208 KB Output is correct
39 Correct 74 ms 25208 KB Output is correct
40 Correct 41 ms 25336 KB Output is correct
41 Correct 40 ms 25208 KB Output is correct
42 Correct 40 ms 25208 KB Output is correct
43 Correct 39 ms 25208 KB Output is correct
44 Correct 39 ms 25208 KB Output is correct
45 Correct 41 ms 25336 KB Output is correct
46 Correct 41 ms 25208 KB Output is correct
47 Correct 40 ms 25208 KB Output is correct
48 Correct 300 ms 38904 KB Output is correct
49 Correct 315 ms 38904 KB Output is correct
50 Correct 294 ms 38904 KB Output is correct
51 Correct 277 ms 38520 KB Output is correct
52 Correct 269 ms 37112 KB Output is correct
53 Correct 239 ms 38264 KB Output is correct
54 Correct 234 ms 37240 KB Output is correct
55 Correct 226 ms 36984 KB Output is correct
56 Correct 219 ms 36856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23800 KB Output is correct
2 Correct 20 ms 23800 KB Output is correct
3 Correct 21 ms 23932 KB Output is correct
4 Correct 20 ms 23800 KB Output is correct
5 Correct 20 ms 23928 KB Output is correct
6 Correct 20 ms 23800 KB Output is correct
7 Correct 21 ms 23800 KB Output is correct
8 Correct 20 ms 23928 KB Output is correct
9 Correct 20 ms 23800 KB Output is correct
10 Correct 19 ms 23928 KB Output is correct
11 Correct 21 ms 23928 KB Output is correct
12 Correct 20 ms 23800 KB Output is correct
13 Correct 20 ms 23800 KB Output is correct
14 Correct 20 ms 23800 KB Output is correct
15 Correct 20 ms 23800 KB Output is correct
16 Correct 20 ms 23800 KB Output is correct
17 Correct 20 ms 23800 KB Output is correct
18 Correct 20 ms 23800 KB Output is correct
19 Correct 20 ms 23800 KB Output is correct
20 Correct 20 ms 23928 KB Output is correct
21 Correct 19 ms 23800 KB Output is correct
22 Correct 20 ms 23800 KB Output is correct
23 Correct 20 ms 23800 KB Output is correct
24 Correct 21 ms 23928 KB Output is correct
25 Correct 23 ms 23804 KB Output is correct
26 Correct 20 ms 23800 KB Output is correct
27 Correct 37 ms 25208 KB Output is correct
28 Correct 35 ms 25448 KB Output is correct
29 Correct 40 ms 25276 KB Output is correct
30 Correct 35 ms 25396 KB Output is correct
31 Correct 36 ms 25208 KB Output is correct
32 Correct 36 ms 25404 KB Output is correct
33 Correct 36 ms 25208 KB Output is correct
34 Correct 37 ms 25464 KB Output is correct
35 Correct 42 ms 25208 KB Output is correct
36 Correct 40 ms 25208 KB Output is correct
37 Correct 46 ms 25208 KB Output is correct
38 Correct 41 ms 25208 KB Output is correct
39 Correct 74 ms 25208 KB Output is correct
40 Correct 41 ms 25336 KB Output is correct
41 Correct 40 ms 25208 KB Output is correct
42 Correct 40 ms 25208 KB Output is correct
43 Correct 39 ms 25208 KB Output is correct
44 Correct 39 ms 25208 KB Output is correct
45 Correct 41 ms 25336 KB Output is correct
46 Correct 41 ms 25208 KB Output is correct
47 Correct 40 ms 25208 KB Output is correct
48 Correct 300 ms 38904 KB Output is correct
49 Correct 315 ms 38904 KB Output is correct
50 Correct 294 ms 38904 KB Output is correct
51 Correct 277 ms 38520 KB Output is correct
52 Correct 269 ms 37112 KB Output is correct
53 Correct 239 ms 38264 KB Output is correct
54 Correct 234 ms 37240 KB Output is correct
55 Correct 226 ms 36984 KB Output is correct
56 Correct 219 ms 36856 KB Output is correct
57 Correct 1174 ms 76920 KB Output is correct
58 Correct 989 ms 59896 KB Output is correct
59 Correct 1046 ms 59960 KB Output is correct
60 Correct 1074 ms 64120 KB Output is correct
61 Correct 1062 ms 64212 KB Output is correct
62 Correct 531 ms 50196 KB Output is correct
63 Correct 721 ms 54648 KB Output is correct
64 Correct 587 ms 51876 KB Output is correct
65 Correct 356 ms 44152 KB Output is correct