답안 #256299

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
256299 2020-08-02T13:55:23 Z pit4h Rope (JOI17_rope) C++14
80 / 100
2500 ms 83284 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;
		vector<vector<int>> cnt(2, vector<int>(m+1));
		
		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((int)best.size(), 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((int)best.size(), (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';
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 416 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 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 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 0 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 416 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 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 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 0 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 10 ms 2048 KB Output is correct
28 Correct 10 ms 1784 KB Output is correct
29 Correct 15 ms 2048 KB Output is correct
30 Correct 11 ms 1648 KB Output is correct
31 Correct 11 ms 2176 KB Output is correct
32 Correct 9 ms 1664 KB Output is correct
33 Correct 9 ms 1920 KB Output is correct
34 Correct 11 ms 1760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 416 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 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 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 0 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 10 ms 2048 KB Output is correct
28 Correct 10 ms 1784 KB Output is correct
29 Correct 15 ms 2048 KB Output is correct
30 Correct 11 ms 1648 KB Output is correct
31 Correct 11 ms 2176 KB Output is correct
32 Correct 9 ms 1664 KB Output is correct
33 Correct 9 ms 1920 KB Output is correct
34 Correct 11 ms 1760 KB Output is correct
35 Correct 15 ms 2176 KB Output is correct
36 Correct 14 ms 2176 KB Output is correct
37 Correct 13 ms 2176 KB Output is correct
38 Correct 14 ms 2176 KB Output is correct
39 Correct 16 ms 2304 KB Output is correct
40 Correct 14 ms 2432 KB Output is correct
41 Correct 13 ms 2468 KB Output is correct
42 Correct 13 ms 2432 KB Output is correct
43 Correct 14 ms 2432 KB Output is correct
44 Correct 13 ms 2432 KB Output is correct
45 Correct 14 ms 2432 KB Output is correct
46 Correct 11 ms 2304 KB Output is correct
47 Correct 11 ms 2432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 416 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 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 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 0 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 10 ms 2048 KB Output is correct
28 Correct 10 ms 1784 KB Output is correct
29 Correct 15 ms 2048 KB Output is correct
30 Correct 11 ms 1648 KB Output is correct
31 Correct 11 ms 2176 KB Output is correct
32 Correct 9 ms 1664 KB Output is correct
33 Correct 9 ms 1920 KB Output is correct
34 Correct 11 ms 1760 KB Output is correct
35 Correct 15 ms 2176 KB Output is correct
36 Correct 14 ms 2176 KB Output is correct
37 Correct 13 ms 2176 KB Output is correct
38 Correct 14 ms 2176 KB Output is correct
39 Correct 16 ms 2304 KB Output is correct
40 Correct 14 ms 2432 KB Output is correct
41 Correct 13 ms 2468 KB Output is correct
42 Correct 13 ms 2432 KB Output is correct
43 Correct 14 ms 2432 KB Output is correct
44 Correct 13 ms 2432 KB Output is correct
45 Correct 14 ms 2432 KB Output is correct
46 Correct 11 ms 2304 KB Output is correct
47 Correct 11 ms 2432 KB Output is correct
48 Correct 187 ms 18232 KB Output is correct
49 Correct 212 ms 18296 KB Output is correct
50 Correct 182 ms 18168 KB Output is correct
51 Correct 164 ms 18168 KB Output is correct
52 Correct 149 ms 15608 KB Output is correct
53 Correct 164 ms 16704 KB Output is correct
54 Correct 142 ms 14712 KB Output is correct
55 Correct 135 ms 14288 KB Output is correct
56 Correct 125 ms 13816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 416 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 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 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 0 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 10 ms 2048 KB Output is correct
28 Correct 10 ms 1784 KB Output is correct
29 Correct 15 ms 2048 KB Output is correct
30 Correct 11 ms 1648 KB Output is correct
31 Correct 11 ms 2176 KB Output is correct
32 Correct 9 ms 1664 KB Output is correct
33 Correct 9 ms 1920 KB Output is correct
34 Correct 11 ms 1760 KB Output is correct
35 Correct 15 ms 2176 KB Output is correct
36 Correct 14 ms 2176 KB Output is correct
37 Correct 13 ms 2176 KB Output is correct
38 Correct 14 ms 2176 KB Output is correct
39 Correct 16 ms 2304 KB Output is correct
40 Correct 14 ms 2432 KB Output is correct
41 Correct 13 ms 2468 KB Output is correct
42 Correct 13 ms 2432 KB Output is correct
43 Correct 14 ms 2432 KB Output is correct
44 Correct 13 ms 2432 KB Output is correct
45 Correct 14 ms 2432 KB Output is correct
46 Correct 11 ms 2304 KB Output is correct
47 Correct 11 ms 2432 KB Output is correct
48 Correct 187 ms 18232 KB Output is correct
49 Correct 212 ms 18296 KB Output is correct
50 Correct 182 ms 18168 KB Output is correct
51 Correct 164 ms 18168 KB Output is correct
52 Correct 149 ms 15608 KB Output is correct
53 Correct 164 ms 16704 KB Output is correct
54 Correct 142 ms 14712 KB Output is correct
55 Correct 135 ms 14288 KB Output is correct
56 Correct 125 ms 13816 KB Output is correct
57 Execution timed out 2580 ms 83284 KB Time limit exceeded
58 Halted 0 ms 0 KB -