Submission #684673

# Submission time Handle Problem Language Result Execution time Memory
684673 2023-01-22T10:07:39 Z ethening Rope (JOI17_rope) C++17
100 / 100
1053 ms 84428 KB
#include "bits/stdc++.h"
#include <utility>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int MAXN = 1000000;
const int MAXM = 1000000;

int n, m;
int a[MAXN + 5];

vector<int> v[MAXM + 5];
int pairing[MAXN + 5];

int ans[MAXM + 5];

int cnt[MAXM + 5];
int cntOcc[MAXN + 5];

int mx;

void del(int x) {
	--cntOcc[cnt[x]];
	if (cntOcc[cnt[x]] == 0 && mx == cnt[x]) --mx;
	++cntOcc[--cnt[x]];
}

void ins(int x) {
	--cntOcc[cnt[x]];
	if (mx == cnt[x]) ++mx;
	++cntOcc[++cnt[x]];
}

void solve() {
	// for (int j = 1; j <= m; j++) {
	// 	cout << "@@" << j << " " << cnt[j] << endl;
	// }
	// for (int j = 1; j <= n; j++) {
	// 	cout << "!@# " << j << " " << cntOcc[j] << endl;
	// }
	// cout << "@@" << mx << endl;
	for (int i = 1; i <= m; i++) {
		int oricol = cnt[i];
		for (int j = 1; j <= oricol; j++) {
			del(i);
		}

		for (int x : v[i]) {
			if (pairing[x] != 0 && a[pairing[x]] != a[x]) {
				del(a[pairing[x]]);
			}
		}

		// for (int j = 1; j <= m; j++) {
		// 	cout << "##" << j << " " << cnt[j] << endl;
		// }

		ans[i] = min(ans[i], n - oricol - mx);
		// cout << "!!!" << i << " " << mx << " " << ans[i] << endl;

		for (int x : v[i]) {
			if (pairing[x] != 0 && a[pairing[x]] != a[x]) {
				ins(a[pairing[x]]);
			}
		}

		for (int j = 1; j <= oricol; j++) {
			ins(i);
		}
	}
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n >> m;

	if (m == 1) {
		cout << "0\n";
		return 0;
	}

	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		v[a[i]].push_back(i);
		++cnt[a[i]];
	}

	for (int i = 1; i <= m; i++) {
		ans[i] = 1e9;
		++cntOcc[cnt[i]];
		mx = max(mx, cnt[i]);
	}

	for (int i = 1; i <= n; i += 2) {
		if (i == n) {
			pairing[i] = 0;
		}
		else {
			pairing[i] = i + 1;
			pairing[i + 1] = i;
		}
	}

	solve();

	pairing[1] = 0;
	for (int i = 2; i <= n; i += 2) {
		if (i == n) {
			pairing[i] = 0;
		}
		else {
			pairing[i] = i + 1;
			pairing[i + 1] = i;
		}
	}

	solve();
 
	for (int i = 1; i <= m; i++) {
		cout << ans[i] << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23764 KB Output is correct
2 Correct 13 ms 23848 KB Output is correct
3 Correct 14 ms 23896 KB Output is correct
4 Correct 13 ms 23844 KB Output is correct
5 Correct 14 ms 23936 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 16 ms 23764 KB Output is correct
9 Correct 13 ms 23772 KB Output is correct
10 Correct 12 ms 23820 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 13 ms 23764 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23824 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23764 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 12 ms 23776 KB Output is correct
21 Correct 12 ms 23848 KB Output is correct
22 Correct 13 ms 23756 KB Output is correct
23 Correct 13 ms 23820 KB Output is correct
24 Correct 15 ms 23740 KB Output is correct
25 Correct 12 ms 23764 KB Output is correct
26 Correct 12 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23764 KB Output is correct
2 Correct 13 ms 23848 KB Output is correct
3 Correct 14 ms 23896 KB Output is correct
4 Correct 13 ms 23844 KB Output is correct
5 Correct 14 ms 23936 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 16 ms 23764 KB Output is correct
9 Correct 13 ms 23772 KB Output is correct
10 Correct 12 ms 23820 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 13 ms 23764 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23824 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23764 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 12 ms 23776 KB Output is correct
21 Correct 12 ms 23848 KB Output is correct
22 Correct 13 ms 23756 KB Output is correct
23 Correct 13 ms 23820 KB Output is correct
24 Correct 15 ms 23740 KB Output is correct
25 Correct 12 ms 23764 KB Output is correct
26 Correct 12 ms 23764 KB Output is correct
27 Correct 24 ms 25428 KB Output is correct
28 Correct 24 ms 25372 KB Output is correct
29 Correct 24 ms 25428 KB Output is correct
30 Correct 28 ms 25532 KB Output is correct
31 Correct 25 ms 25408 KB Output is correct
32 Correct 27 ms 25536 KB Output is correct
33 Correct 25 ms 25372 KB Output is correct
34 Correct 26 ms 25628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23764 KB Output is correct
2 Correct 13 ms 23848 KB Output is correct
3 Correct 14 ms 23896 KB Output is correct
4 Correct 13 ms 23844 KB Output is correct
5 Correct 14 ms 23936 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 16 ms 23764 KB Output is correct
9 Correct 13 ms 23772 KB Output is correct
10 Correct 12 ms 23820 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 13 ms 23764 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23824 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23764 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 12 ms 23776 KB Output is correct
21 Correct 12 ms 23848 KB Output is correct
22 Correct 13 ms 23756 KB Output is correct
23 Correct 13 ms 23820 KB Output is correct
24 Correct 15 ms 23740 KB Output is correct
25 Correct 12 ms 23764 KB Output is correct
26 Correct 12 ms 23764 KB Output is correct
27 Correct 24 ms 25428 KB Output is correct
28 Correct 24 ms 25372 KB Output is correct
29 Correct 24 ms 25428 KB Output is correct
30 Correct 28 ms 25532 KB Output is correct
31 Correct 25 ms 25408 KB Output is correct
32 Correct 27 ms 25536 KB Output is correct
33 Correct 25 ms 25372 KB Output is correct
34 Correct 26 ms 25628 KB Output is correct
35 Correct 30 ms 25508 KB Output is correct
36 Correct 33 ms 25556 KB Output is correct
37 Correct 33 ms 25560 KB Output is correct
38 Correct 31 ms 25600 KB Output is correct
39 Correct 29 ms 25580 KB Output is correct
40 Correct 28 ms 25528 KB Output is correct
41 Correct 28 ms 25608 KB Output is correct
42 Correct 26 ms 25612 KB Output is correct
43 Correct 26 ms 25580 KB Output is correct
44 Correct 27 ms 25624 KB Output is correct
45 Correct 27 ms 25612 KB Output is correct
46 Correct 29 ms 25556 KB Output is correct
47 Correct 26 ms 25584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23764 KB Output is correct
2 Correct 13 ms 23848 KB Output is correct
3 Correct 14 ms 23896 KB Output is correct
4 Correct 13 ms 23844 KB Output is correct
5 Correct 14 ms 23936 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 16 ms 23764 KB Output is correct
9 Correct 13 ms 23772 KB Output is correct
10 Correct 12 ms 23820 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 13 ms 23764 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23824 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23764 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 12 ms 23776 KB Output is correct
21 Correct 12 ms 23848 KB Output is correct
22 Correct 13 ms 23756 KB Output is correct
23 Correct 13 ms 23820 KB Output is correct
24 Correct 15 ms 23740 KB Output is correct
25 Correct 12 ms 23764 KB Output is correct
26 Correct 12 ms 23764 KB Output is correct
27 Correct 24 ms 25428 KB Output is correct
28 Correct 24 ms 25372 KB Output is correct
29 Correct 24 ms 25428 KB Output is correct
30 Correct 28 ms 25532 KB Output is correct
31 Correct 25 ms 25408 KB Output is correct
32 Correct 27 ms 25536 KB Output is correct
33 Correct 25 ms 25372 KB Output is correct
34 Correct 26 ms 25628 KB Output is correct
35 Correct 30 ms 25508 KB Output is correct
36 Correct 33 ms 25556 KB Output is correct
37 Correct 33 ms 25560 KB Output is correct
38 Correct 31 ms 25600 KB Output is correct
39 Correct 29 ms 25580 KB Output is correct
40 Correct 28 ms 25528 KB Output is correct
41 Correct 28 ms 25608 KB Output is correct
42 Correct 26 ms 25612 KB Output is correct
43 Correct 26 ms 25580 KB Output is correct
44 Correct 27 ms 25624 KB Output is correct
45 Correct 27 ms 25612 KB Output is correct
46 Correct 29 ms 25556 KB Output is correct
47 Correct 26 ms 25584 KB Output is correct
48 Correct 286 ms 42852 KB Output is correct
49 Correct 317 ms 42832 KB Output is correct
50 Correct 275 ms 42776 KB Output is correct
51 Correct 256 ms 42416 KB Output is correct
52 Correct 239 ms 41200 KB Output is correct
53 Correct 185 ms 42020 KB Output is correct
54 Correct 186 ms 41020 KB Output is correct
55 Correct 192 ms 40852 KB Output is correct
56 Correct 174 ms 40568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23764 KB Output is correct
2 Correct 13 ms 23848 KB Output is correct
3 Correct 14 ms 23896 KB Output is correct
4 Correct 13 ms 23844 KB Output is correct
5 Correct 14 ms 23936 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 16 ms 23764 KB Output is correct
9 Correct 13 ms 23772 KB Output is correct
10 Correct 12 ms 23820 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 13 ms 23764 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23824 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 13 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23764 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 12 ms 23776 KB Output is correct
21 Correct 12 ms 23848 KB Output is correct
22 Correct 13 ms 23756 KB Output is correct
23 Correct 13 ms 23820 KB Output is correct
24 Correct 15 ms 23740 KB Output is correct
25 Correct 12 ms 23764 KB Output is correct
26 Correct 12 ms 23764 KB Output is correct
27 Correct 24 ms 25428 KB Output is correct
28 Correct 24 ms 25372 KB Output is correct
29 Correct 24 ms 25428 KB Output is correct
30 Correct 28 ms 25532 KB Output is correct
31 Correct 25 ms 25408 KB Output is correct
32 Correct 27 ms 25536 KB Output is correct
33 Correct 25 ms 25372 KB Output is correct
34 Correct 26 ms 25628 KB Output is correct
35 Correct 30 ms 25508 KB Output is correct
36 Correct 33 ms 25556 KB Output is correct
37 Correct 33 ms 25560 KB Output is correct
38 Correct 31 ms 25600 KB Output is correct
39 Correct 29 ms 25580 KB Output is correct
40 Correct 28 ms 25528 KB Output is correct
41 Correct 28 ms 25608 KB Output is correct
42 Correct 26 ms 25612 KB Output is correct
43 Correct 26 ms 25580 KB Output is correct
44 Correct 27 ms 25624 KB Output is correct
45 Correct 27 ms 25612 KB Output is correct
46 Correct 29 ms 25556 KB Output is correct
47 Correct 26 ms 25584 KB Output is correct
48 Correct 286 ms 42852 KB Output is correct
49 Correct 317 ms 42832 KB Output is correct
50 Correct 275 ms 42776 KB Output is correct
51 Correct 256 ms 42416 KB Output is correct
52 Correct 239 ms 41200 KB Output is correct
53 Correct 185 ms 42020 KB Output is correct
54 Correct 186 ms 41020 KB Output is correct
55 Correct 192 ms 40852 KB Output is correct
56 Correct 174 ms 40568 KB Output is correct
57 Correct 1053 ms 84428 KB Output is correct
58 Correct 921 ms 66136 KB Output is correct
59 Correct 878 ms 65940 KB Output is correct
60 Correct 949 ms 70624 KB Output is correct
61 Correct 892 ms 70672 KB Output is correct
62 Correct 479 ms 55016 KB Output is correct
63 Correct 620 ms 60232 KB Output is correct
64 Correct 513 ms 57036 KB Output is correct
65 Correct 282 ms 48600 KB Output is correct