답안 #820209

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
820209 2023-08-11T01:20:26 Z Zanite Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
384 ms 200440 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 1 ms 332 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 0 ms 324 KB Output is correct
14 Correct 1 ms 584 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 0 ms 324 KB Output is correct
14 Correct 1 ms 584 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 4556 KB Output is correct
19 Correct 3 ms 4564 KB Output is correct
20 Correct 3 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 3 ms 4564 KB Output is correct
24 Correct 3 ms 4556 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 3 ms 4564 KB Output is correct
28 Correct 3 ms 4564 KB Output is correct
29 Correct 3 ms 4564 KB Output is correct
30 Correct 3 ms 4564 KB Output is correct
31 Correct 3 ms 4564 KB Output is correct
32 Correct 3 ms 4564 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 3 ms 4300 KB Output is correct
36 Correct 2 ms 4564 KB Output is correct
37 Correct 2 ms 4308 KB Output is correct
38 Correct 3 ms 4564 KB Output is correct
39 Correct 3 ms 4556 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 326 ms 200376 KB Output is correct
3 Correct 337 ms 200380 KB Output is correct
4 Correct 345 ms 200332 KB Output is correct
5 Correct 355 ms 200404 KB Output is correct
6 Correct 336 ms 200352 KB Output is correct
7 Correct 332 ms 200268 KB Output is correct
8 Correct 327 ms 200288 KB Output is correct
9 Correct 327 ms 198436 KB Output is correct
10 Correct 347 ms 200388 KB Output is correct
11 Correct 357 ms 200364 KB Output is correct
12 Correct 60 ms 53196 KB Output is correct
13 Correct 137 ms 94140 KB Output is correct
14 Correct 200 ms 136480 KB Output is correct
15 Correct 1 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 1 ms 332 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 0 ms 324 KB Output is correct
14 Correct 1 ms 584 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 4556 KB Output is correct
19 Correct 3 ms 4564 KB Output is correct
20 Correct 3 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 3 ms 4564 KB Output is correct
24 Correct 3 ms 4556 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 3 ms 4564 KB Output is correct
28 Correct 3 ms 4564 KB Output is correct
29 Correct 3 ms 4564 KB Output is correct
30 Correct 3 ms 4564 KB Output is correct
31 Correct 3 ms 4564 KB Output is correct
32 Correct 3 ms 4564 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 3 ms 4300 KB Output is correct
36 Correct 2 ms 4564 KB Output is correct
37 Correct 2 ms 4308 KB Output is correct
38 Correct 3 ms 4564 KB Output is correct
39 Correct 3 ms 4556 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 326 ms 200376 KB Output is correct
44 Correct 337 ms 200380 KB Output is correct
45 Correct 345 ms 200332 KB Output is correct
46 Correct 355 ms 200404 KB Output is correct
47 Correct 336 ms 200352 KB Output is correct
48 Correct 332 ms 200268 KB Output is correct
49 Correct 327 ms 200288 KB Output is correct
50 Correct 327 ms 198436 KB Output is correct
51 Correct 347 ms 200388 KB Output is correct
52 Correct 357 ms 200364 KB Output is correct
53 Correct 60 ms 53196 KB Output is correct
54 Correct 137 ms 94140 KB Output is correct
55 Correct 200 ms 136480 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 0 ms 340 KB Output is correct
58 Correct 359 ms 200352 KB Output is correct
59 Correct 341 ms 200372 KB Output is correct
60 Correct 333 ms 198356 KB Output is correct
61 Correct 365 ms 200256 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 341 ms 200376 KB Output is correct
64 Correct 384 ms 200324 KB Output is correct
65 Correct 334 ms 200368 KB Output is correct
66 Correct 360 ms 198364 KB Output is correct
67 Correct 350 ms 200440 KB Output is correct
68 Correct 346 ms 200348 KB Output is correct
69 Correct 342 ms 200384 KB Output is correct
70 Correct 363 ms 200416 KB Output is correct
71 Correct 337 ms 200312 KB Output is correct
72 Correct 0 ms 340 KB Output is correct
73 Correct 0 ms 336 KB Output is correct