Submission #256304

# Submission time Handle Problem Language Result Execution time Memory
256304 2020-08-02T14:00:28 Z pit4h Rope (JOI17_rope) C++14
100 / 100
1246 ms 78032 KB
#include<bits/stdc++.h>
#define st first
#define nd second
using namespace std;

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n, m;
	cin>>n>>m;
	vector<int> a(n), occ(m+1);
	vector<vector<pair<int, int>>> col(m+1);
	vector<pair<int, int>> best;
	for(int i=0; i<n; ++i) {
		cin>>a[i];
		occ[a[i]]++;
		if(i==0 || a[i] != a[i-1]) {
			col[a[i]].push_back({i, i});
		}
		else {
			col[a[i]].back().nd = i;
		}
	}
	for(int i=1; i<=m; ++i) {
		best.push_back({occ[i], i});
	}
	sort(best.rbegin(), best.rend());
	for(int c=1; c<=m; ++c) {
		if(col[c].empty()) continue;
		map<int, int> cnt[2];
		
		vector<int> prv(2);
		vector<bool> fail(2);

		int beg = col[c][0].st;
		int end = col[c][0].nd;
		if(end==n-1) {
			int ans = n;
			for(int i=0; i<min(m, 2); ++i) {
				if(c==best[i].nd) ans=min(ans, n-occ[c]);
				else ans = min(ans, n-occ[c]-occ[best[i].nd]);
			}
			cout<<ans<<'\n';
			continue;
		}
		if((end-beg+1)%2==1) {
			if(beg==0) {
				prv[0] = end;
				cnt[1][a[end+1]] = 1;
				prv[1] = end+1;
			}
			else {
				cnt[0][a[beg-1]] = 1;
				prv[0] = end;
				cnt[1][a[end+1]] = 1;
				prv[1] = end+1;
			}
		}
		else {
			if(beg==0) {
				prv[0] = end;
				cnt[1][a[end+1]] = 1;
				prv[1] = end+1;
			}
			else {
				prv[0] = end;
				cnt[1][a[beg-1]] = 1;
				cnt[1][a[end+1]] = 1;
				prv[1] = end+1;
			}
		}
		for(int it=0; it<=1; ++it) {
			bool start=0;
			for(auto cur: col[c]) {
				if(!start) {
					start=1;
					continue;
				}
				beg = cur.st;
				end = cur.nd;
				if((beg-prv[it])%2==0) {
					beg--;
					cnt[it][a[beg]]++;
				}
				if((end-beg+1)%2==1 && end != n-1) {
					end++;
					cnt[it][a[end]]++;
				}
				prv[it] = end;
			}
		}
		int ans = n;
		for(int j=0; j<min(m, (int)col[c].size()+1); ++j) {
			int i = best[j].nd;
			if(c==i) {
				ans = min(ans, n-occ[c]);
			}
			else {
				if(!fail[0]) ans = min(ans, n - occ[c] - occ[i] + cnt[0][i]);
				if(!fail[1]) ans = min(ans, n - occ[c] - occ[i] + cnt[1][i]);
			}
		}
		cout<<ans<<'\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 0 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 0 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
27 Correct 14 ms 1792 KB Output is correct
28 Correct 12 ms 1656 KB Output is correct
29 Correct 13 ms 1780 KB Output is correct
30 Correct 12 ms 1520 KB Output is correct
31 Correct 15 ms 1984 KB Output is correct
32 Correct 11 ms 1532 KB Output is correct
33 Correct 13 ms 1792 KB Output is correct
34 Correct 12 ms 1632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 0 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
27 Correct 14 ms 1792 KB Output is correct
28 Correct 12 ms 1656 KB Output is correct
29 Correct 13 ms 1780 KB Output is correct
30 Correct 12 ms 1520 KB Output is correct
31 Correct 15 ms 1984 KB Output is correct
32 Correct 11 ms 1532 KB Output is correct
33 Correct 13 ms 1792 KB Output is correct
34 Correct 12 ms 1632 KB Output is correct
35 Correct 61 ms 1792 KB Output is correct
36 Correct 62 ms 1792 KB Output is correct
37 Correct 64 ms 1860 KB Output is correct
38 Correct 66 ms 1792 KB Output is correct
39 Correct 64 ms 1792 KB Output is correct
40 Correct 50 ms 2048 KB Output is correct
41 Correct 49 ms 2048 KB Output is correct
42 Correct 41 ms 1920 KB Output is correct
43 Correct 39 ms 2048 KB Output is correct
44 Correct 49 ms 2048 KB Output is correct
45 Correct 51 ms 2048 KB Output is correct
46 Correct 41 ms 1920 KB Output is correct
47 Correct 42 ms 2048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 0 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
27 Correct 14 ms 1792 KB Output is correct
28 Correct 12 ms 1656 KB Output is correct
29 Correct 13 ms 1780 KB Output is correct
30 Correct 12 ms 1520 KB Output is correct
31 Correct 15 ms 1984 KB Output is correct
32 Correct 11 ms 1532 KB Output is correct
33 Correct 13 ms 1792 KB Output is correct
34 Correct 12 ms 1632 KB Output is correct
35 Correct 61 ms 1792 KB Output is correct
36 Correct 62 ms 1792 KB Output is correct
37 Correct 64 ms 1860 KB Output is correct
38 Correct 66 ms 1792 KB Output is correct
39 Correct 64 ms 1792 KB Output is correct
40 Correct 50 ms 2048 KB Output is correct
41 Correct 49 ms 2048 KB Output is correct
42 Correct 41 ms 1920 KB Output is correct
43 Correct 39 ms 2048 KB Output is correct
44 Correct 49 ms 2048 KB Output is correct
45 Correct 51 ms 2048 KB Output is correct
46 Correct 41 ms 1920 KB Output is correct
47 Correct 42 ms 2048 KB Output is correct
48 Correct 822 ms 17780 KB Output is correct
49 Correct 793 ms 17660 KB Output is correct
50 Correct 792 ms 17656 KB Output is correct
51 Correct 750 ms 17400 KB Output is correct
52 Correct 665 ms 14844 KB Output is correct
53 Correct 627 ms 16120 KB Output is correct
54 Correct 633 ms 14044 KB Output is correct
55 Correct 643 ms 13712 KB Output is correct
56 Correct 587 ms 13176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 0 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
27 Correct 14 ms 1792 KB Output is correct
28 Correct 12 ms 1656 KB Output is correct
29 Correct 13 ms 1780 KB Output is correct
30 Correct 12 ms 1520 KB Output is correct
31 Correct 15 ms 1984 KB Output is correct
32 Correct 11 ms 1532 KB Output is correct
33 Correct 13 ms 1792 KB Output is correct
34 Correct 12 ms 1632 KB Output is correct
35 Correct 61 ms 1792 KB Output is correct
36 Correct 62 ms 1792 KB Output is correct
37 Correct 64 ms 1860 KB Output is correct
38 Correct 66 ms 1792 KB Output is correct
39 Correct 64 ms 1792 KB Output is correct
40 Correct 50 ms 2048 KB Output is correct
41 Correct 49 ms 2048 KB Output is correct
42 Correct 41 ms 1920 KB Output is correct
43 Correct 39 ms 2048 KB Output is correct
44 Correct 49 ms 2048 KB Output is correct
45 Correct 51 ms 2048 KB Output is correct
46 Correct 41 ms 1920 KB Output is correct
47 Correct 42 ms 2048 KB Output is correct
48 Correct 822 ms 17780 KB Output is correct
49 Correct 793 ms 17660 KB Output is correct
50 Correct 792 ms 17656 KB Output is correct
51 Correct 750 ms 17400 KB Output is correct
52 Correct 665 ms 14844 KB Output is correct
53 Correct 627 ms 16120 KB Output is correct
54 Correct 633 ms 14044 KB Output is correct
55 Correct 643 ms 13712 KB Output is correct
56 Correct 587 ms 13176 KB Output is correct
57 Correct 1210 ms 78032 KB Output is correct
58 Correct 1155 ms 56740 KB Output is correct
59 Correct 1122 ms 56788 KB Output is correct
60 Correct 1196 ms 63544 KB Output is correct
61 Correct 1246 ms 63572 KB Output is correct
62 Correct 756 ms 39384 KB Output is correct
63 Correct 908 ms 47460 KB Output is correct
64 Correct 804 ms 42464 KB Output is correct
65 Correct 633 ms 29544 KB Output is correct