답안 #173184

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
173184 2020-01-03T14:39:50 Z songc Rope (JOI17_rope) C++14
100 / 100
791 ms 41484 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

int N, M;
int A[1010101];
int Ecnt[1010101], E[1010101], EM, ES, EN=1;
int Ocnt[1010101], O[1010101], OM, OS, ON=1;
pii Ec[1010101], Oc[1010101];

int Eup(int k, int v){
	Ecnt[E[k]]--;
	if (v < 0 ){
		if (!Ecnt[EM]) EM--;
		else if (EM == ES && Ecnt[EM]<=1) ES--;
		else if (!Ecnt[ES]) ES--;
	}
	E[k] += v;
	if (v > 0){
		if (EM < E[k]) EM = E[k];
		else if (ES < E[k]) ES = E[k];
	}
	Ecnt[E[k]]++;
}

int Oup(int k, int v){
	Ocnt[O[k]]--;
	if (v < 0){
		if (!Ocnt[OM]) OM--;
		else if (OM == OS && Ocnt[OM]<=1) OS--;
		else if (!Ocnt[OS]) OS--;
	}
	O[k] += v;
	if (v > 0){
		if (OM < O[k]) OM = O[k];
		else if (OS < O[k]) OS = O[k];
	}
	Ocnt[O[k]]++;
}

int main(){
	scanf("%d %d", &N, &M);
	if (M == 1){
		puts("0");
		return 0;
	}
	Ecnt[0]=Ocnt[0]=M;
	for (int i=1; i<=N; i++) scanf("%d", &A[i]);

	for (int i=1; i+1<=N; i+=2){
		Eup(A[i], 1);
		Eup(A[i+1], 1);
		if (A[i] != A[i+1]){
			Ec[EN++] = pii(A[i], A[i+1]);
			Ec[EN++] = pii(A[i+1], A[i]);
		}
	}
	for (int i=2; i+1<=N; i+=2){
		Oup(A[i], 1);
		Oup(A[i+1], 1);
		if (A[i] != A[i+1]){
			Oc[ON++] = pii(A[i], A[i+1]);
			Oc[ON++] = pii(A[i+1], A[i]);
		}
	}

	if (N & 1){
		Eup(A[N], 1);
		Oup(A[1], 1);
	}
	else{
		Oup(A[1], 1);
		Oup(A[N], 1);
	}
	sort(Ec+1, Ec+EN);
	sort(Oc+1, Oc+ON);

	int te=1, to=1; 
	for (int i=1; i<=M; i++){
		int ans=0;
		for (int j=te; j<EN && Ec[j].first==i; j++) Eup(Ec[j].second, -1);
		for (int j=to; j<ON && Oc[j].first==i; j++) Oup(Oc[j].second, -1);

		if (E[i] == EM) ans = max(ans, E[i]+ES);
		else ans = max(ans, E[i]+EM);
		if (O[i] == OM) ans = max(ans, O[i]+OS);
		else ans = max(ans, O[i]+OM);
		printf("%d\n", N-ans);

		for (; te<EN && Ec[te].first==i; te++) Eup(Ec[te].second, 1);
		for (; to<ON && Oc[to].first==i; to++) Oup(Oc[to].second, 1);
	}
	return 0;
}

Compilation message

rope.cpp: In function 'int Eup(int, int)':
rope.cpp:25:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
rope.cpp: In function 'int Oup(int, int)':
rope.cpp:40:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
rope.cpp: In function 'int main()':
rope.cpp:43:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~~
rope.cpp:49:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i=1; i<=N; i++) scanf("%d", &A[i]);
                           ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 380 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 380 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 252 KB Output is correct
27 Correct 29 ms 2536 KB Output is correct
28 Correct 24 ms 2296 KB Output is correct
29 Correct 28 ms 2424 KB Output is correct
30 Correct 25 ms 2296 KB Output is correct
31 Correct 29 ms 2424 KB Output is correct
32 Correct 23 ms 2296 KB Output is correct
33 Correct 28 ms 2424 KB Output is correct
34 Correct 25 ms 2296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 380 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 252 KB Output is correct
27 Correct 29 ms 2536 KB Output is correct
28 Correct 24 ms 2296 KB Output is correct
29 Correct 28 ms 2424 KB Output is correct
30 Correct 25 ms 2296 KB Output is correct
31 Correct 29 ms 2424 KB Output is correct
32 Correct 23 ms 2296 KB Output is correct
33 Correct 28 ms 2424 KB Output is correct
34 Correct 25 ms 2296 KB Output is correct
35 Correct 41 ms 2680 KB Output is correct
36 Correct 40 ms 2744 KB Output is correct
37 Correct 41 ms 2748 KB Output is correct
38 Correct 39 ms 2680 KB Output is correct
39 Correct 41 ms 2688 KB Output is correct
40 Correct 38 ms 2680 KB Output is correct
41 Correct 39 ms 2776 KB Output is correct
42 Correct 38 ms 2680 KB Output is correct
43 Correct 40 ms 2720 KB Output is correct
44 Correct 38 ms 2680 KB Output is correct
45 Correct 38 ms 2680 KB Output is correct
46 Correct 37 ms 2652 KB Output is correct
47 Correct 38 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 380 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 252 KB Output is correct
27 Correct 29 ms 2536 KB Output is correct
28 Correct 24 ms 2296 KB Output is correct
29 Correct 28 ms 2424 KB Output is correct
30 Correct 25 ms 2296 KB Output is correct
31 Correct 29 ms 2424 KB Output is correct
32 Correct 23 ms 2296 KB Output is correct
33 Correct 28 ms 2424 KB Output is correct
34 Correct 25 ms 2296 KB Output is correct
35 Correct 41 ms 2680 KB Output is correct
36 Correct 40 ms 2744 KB Output is correct
37 Correct 41 ms 2748 KB Output is correct
38 Correct 39 ms 2680 KB Output is correct
39 Correct 41 ms 2688 KB Output is correct
40 Correct 38 ms 2680 KB Output is correct
41 Correct 39 ms 2776 KB Output is correct
42 Correct 38 ms 2680 KB Output is correct
43 Correct 40 ms 2720 KB Output is correct
44 Correct 38 ms 2680 KB Output is correct
45 Correct 38 ms 2680 KB Output is correct
46 Correct 37 ms 2652 KB Output is correct
47 Correct 38 ms 2680 KB Output is correct
48 Correct 430 ms 24528 KB Output is correct
49 Correct 435 ms 24520 KB Output is correct
50 Correct 430 ms 24524 KB Output is correct
51 Correct 428 ms 24216 KB Output is correct
52 Correct 420 ms 23888 KB Output is correct
53 Correct 412 ms 24572 KB Output is correct
54 Correct 405 ms 24484 KB Output is correct
55 Correct 405 ms 24504 KB Output is correct
56 Correct 407 ms 24568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 380 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 252 KB Output is correct
27 Correct 29 ms 2536 KB Output is correct
28 Correct 24 ms 2296 KB Output is correct
29 Correct 28 ms 2424 KB Output is correct
30 Correct 25 ms 2296 KB Output is correct
31 Correct 29 ms 2424 KB Output is correct
32 Correct 23 ms 2296 KB Output is correct
33 Correct 28 ms 2424 KB Output is correct
34 Correct 25 ms 2296 KB Output is correct
35 Correct 41 ms 2680 KB Output is correct
36 Correct 40 ms 2744 KB Output is correct
37 Correct 41 ms 2748 KB Output is correct
38 Correct 39 ms 2680 KB Output is correct
39 Correct 41 ms 2688 KB Output is correct
40 Correct 38 ms 2680 KB Output is correct
41 Correct 39 ms 2776 KB Output is correct
42 Correct 38 ms 2680 KB Output is correct
43 Correct 40 ms 2720 KB Output is correct
44 Correct 38 ms 2680 KB Output is correct
45 Correct 38 ms 2680 KB Output is correct
46 Correct 37 ms 2652 KB Output is correct
47 Correct 38 ms 2680 KB Output is correct
48 Correct 430 ms 24528 KB Output is correct
49 Correct 435 ms 24520 KB Output is correct
50 Correct 430 ms 24524 KB Output is correct
51 Correct 428 ms 24216 KB Output is correct
52 Correct 420 ms 23888 KB Output is correct
53 Correct 412 ms 24572 KB Output is correct
54 Correct 405 ms 24484 KB Output is correct
55 Correct 405 ms 24504 KB Output is correct
56 Correct 407 ms 24568 KB Output is correct
57 Correct 791 ms 41484 KB Output is correct
58 Correct 717 ms 35460 KB Output is correct
59 Correct 663 ms 35384 KB Output is correct
60 Correct 704 ms 36984 KB Output is correct
61 Correct 719 ms 36988 KB Output is correct
62 Correct 519 ms 30844 KB Output is correct
63 Correct 560 ms 32836 KB Output is correct
64 Correct 529 ms 31608 KB Output is correct
65 Correct 459 ms 28340 KB Output is correct