답안 #820210

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
820210 2023-08-11T01:21:05 Z vjudge1 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
381 ms 200428 KB
#include <bits/stdc++.h>
using namespace std;
 
array<int, 255> colorCode;
const int iINF	= 1e9;
const int maxN	= 423;
 
int N, cnt[3], color[maxN], pos[3][maxN], pf[3][maxN];
int dp[maxN][maxN][maxN][3];
 
int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	colorCode['R'] = 0;
	colorCode['G'] = 1;
	colorCode['Y'] = 2;
 
	cin >> N;
	char buf;
	for (int i = 1; i <= N; i++) {
		cin >> buf;
		color[i] = colorCode[buf];
	}
 
	#define R	cnt[0]
	#define G	cnt[1]
	#define Y	cnt[2]
 
	#define r	lpos[0]
	#define g	lpos[1]
	#define y	lpos[2]
	
	for (int i = 1; i <= N; i++) {
		pos[color[i]][++cnt[color[i]]] = i;
		for (int j = 0; j <= 2; j++) {
			pf[j][i] = pf[j][i-1] + (color[i] == j);
		}
	}
 
	int bound	= (N + 1) / 2;
	if (max({R, G, Y}) > bound) {
		cout << -1 << '\n';
		return 0;
	}
 
	// dp[r][g][y][lst] = number of swaps to the left that must be done
	// to get r red, g green, and y yellow with last color lst
	array<int, 3> lpos;
	for (int n = 1; n <= N; n++) {
		for (r = 0; r <= bound; r++) {
			for (g = 0; g <= min(n-r, bound); g++) {
				y = n-r-g;
				for (int lst = 0; lst <= 2; lst++) {
					if (!lpos[lst]) {
						dp[r][g][y][lst] = iINF;
						continue;
					}
 
					int prv = iINF;
					lpos[lst]--;
					for (int plst = 0; plst <= 2; plst++) {
						if (plst == lst) continue;
						prv = min(prv, dp[r][g][y][plst]);
					}
					lpos[lst]++;
 
					int swaps = 0;
					for (int i = 0; i <= 2; i++) {
						if (i == lst) continue;
						swaps += max(0, pf[i][pos[lst][lpos[lst]]] - lpos[i]);
					}
 
					dp[r][g][y][lst] = min(iINF, prv + swaps);
				}
			}
		}
	}
 
	int ans = iINF;
	for (int lst = 0; lst <= 2; lst++) ans = min(ans, dp[R][G][Y][lst]);
	if (ans == iINF) ans = -1;
	cout << ans << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 0 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 0 ms 596 KB Output is correct
9 Correct 0 ms 596 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 596 KB Output is correct
12 Correct 0 ms 596 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 0 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 0 ms 596 KB Output is correct
9 Correct 0 ms 596 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 596 KB Output is correct
12 Correct 0 ms 596 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 3 ms 4564 KB Output is correct
19 Correct 2 ms 4564 KB Output is correct
20 Correct 2 ms 4564 KB Output is correct
21 Correct 3 ms 4564 KB Output is correct
22 Correct 3 ms 4564 KB Output is correct
23 Correct 2 ms 4564 KB Output is correct
24 Correct 2 ms 4564 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 2 ms 4564 KB Output is correct
28 Correct 2 ms 4564 KB Output is correct
29 Correct 2 ms 4564 KB Output is correct
30 Correct 3 ms 4564 KB Output is correct
31 Correct 2 ms 4564 KB Output is correct
32 Correct 2 ms 4564 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 2 ms 4308 KB Output is correct
36 Correct 3 ms 4620 KB Output is correct
37 Correct 2 ms 4308 KB Output is correct
38 Correct 2 ms 4564 KB Output is correct
39 Correct 2 ms 4564 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 340 ms 200396 KB Output is correct
3 Correct 334 ms 200252 KB Output is correct
4 Correct 338 ms 200400 KB Output is correct
5 Correct 334 ms 200332 KB Output is correct
6 Correct 356 ms 200316 KB Output is correct
7 Correct 339 ms 200272 KB Output is correct
8 Correct 338 ms 200284 KB Output is correct
9 Correct 381 ms 198320 KB Output is correct
10 Correct 342 ms 200428 KB Output is correct
11 Correct 325 ms 200396 KB Output is correct
12 Correct 63 ms 53272 KB Output is correct
13 Correct 124 ms 94292 KB Output is correct
14 Correct 210 ms 136484 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 0 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 0 ms 596 KB Output is correct
9 Correct 0 ms 596 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 596 KB Output is correct
12 Correct 0 ms 596 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 3 ms 4564 KB Output is correct
19 Correct 2 ms 4564 KB Output is correct
20 Correct 2 ms 4564 KB Output is correct
21 Correct 3 ms 4564 KB Output is correct
22 Correct 3 ms 4564 KB Output is correct
23 Correct 2 ms 4564 KB Output is correct
24 Correct 2 ms 4564 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 2 ms 4564 KB Output is correct
28 Correct 2 ms 4564 KB Output is correct
29 Correct 2 ms 4564 KB Output is correct
30 Correct 3 ms 4564 KB Output is correct
31 Correct 2 ms 4564 KB Output is correct
32 Correct 2 ms 4564 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 2 ms 4308 KB Output is correct
36 Correct 3 ms 4620 KB Output is correct
37 Correct 2 ms 4308 KB Output is correct
38 Correct 2 ms 4564 KB Output is correct
39 Correct 2 ms 4564 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 340 ms 200396 KB Output is correct
44 Correct 334 ms 200252 KB Output is correct
45 Correct 338 ms 200400 KB Output is correct
46 Correct 334 ms 200332 KB Output is correct
47 Correct 356 ms 200316 KB Output is correct
48 Correct 339 ms 200272 KB Output is correct
49 Correct 338 ms 200284 KB Output is correct
50 Correct 381 ms 198320 KB Output is correct
51 Correct 342 ms 200428 KB Output is correct
52 Correct 325 ms 200396 KB Output is correct
53 Correct 63 ms 53272 KB Output is correct
54 Correct 124 ms 94292 KB Output is correct
55 Correct 210 ms 136484 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 Correct 0 ms 340 KB Output is correct
58 Correct 355 ms 200372 KB Output is correct
59 Correct 355 ms 200392 KB Output is correct
60 Correct 331 ms 198360 KB Output is correct
61 Correct 345 ms 200364 KB Output is correct
62 Correct 0 ms 340 KB Output is correct
63 Correct 333 ms 200388 KB Output is correct
64 Correct 342 ms 200408 KB Output is correct
65 Correct 332 ms 200320 KB Output is correct
66 Correct 333 ms 198328 KB Output is correct
67 Correct 333 ms 200400 KB Output is correct
68 Correct 337 ms 200296 KB Output is correct
69 Correct 332 ms 200392 KB Output is correct
70 Correct 339 ms 200376 KB Output is correct
71 Correct 333 ms 200296 KB Output is correct
72 Correct 0 ms 340 KB Output is correct
73 Correct 0 ms 340 KB Output is correct