답안 #252709

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
252709 2020-07-26T07:15:19 Z Mlxa Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
60 / 100
11 ms 11776 KB
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define x first
#define y second
using namespace std;
using ll  = long long;
#define int ll

void umin(int &a, int b) {
	a = min(a, b);
}
void umax(int &a, int b) {
	a = max(a, b);
}

const int N   = 62;
const int INF = 1e18;

int n;
string str;
int a[N];
int pr[N];
int pg[N];
int py[N];
int wr[N];
int wg[N];
int wy[N];
int dp[N][N][N][3];
// R, G, Y, Last

signed main() {
#ifdef LC
    assert(freopen("input.txt", "r", stdin));
#endif
    ios::sync_with_stdio(0), cin.tie(0);

	cin >> n >> str;
	for (int i = 0; i < n; ++i) {
		pr[i + 1] = pr[i];
		pg[i + 1] = pg[i];
		py[i + 1] = py[i];
		if (str[i] == 'R') {
			a[i] = 0;
			wr[pr[i]] = i;
			++pr[i + 1];
		} else if (str[i] == 'G') {
			a[i] = 1;
			wg[pg[i]] = i;
			++pg[i + 1];
		} else {
			assert(str[i] == 'Y');
			a[i] = 2;
			wy[py[i]] = i;
			++py[i + 1];
		}
	}
	fill_n(dp[0][0][0], 3 * N * N * N, INF);
	dp[0][0][0][0] = 0;
	dp[0][0][0][1] = 0;
	dp[0][0][0][2] = 0;
	for (int r = 0; r <= pr[n]; ++r) {
		for (int g = 0; g <= pg[n]; ++g) {
			for (int y = 0; y <= py[n]; ++y) {
				for (int l = 0; l < 3; ++l) {
					int v = dp[r][g][y][l];
					if (l != 0 && r < pr[n]) {
						int i = wr[r];
						umin(dp[r + 1][g][y][0], v + abs(pg[i] - g) + abs(py[i] - y));
					}
					if (l != 1 && g < pg[n]) {
						int i = wg[g];
						umin(dp[r][g + 1][y][1], v + abs(pr[i] - r) + abs(py[i] - y));
					}
					if (l != 2 && y < py[n]) {
						int i = wy[y];
						umin(dp[r][g][y + 1][2], v + abs(pr[i] - r) + abs(pg[i] - g));
					}
				}
			}
		}
	}
	int r = pr[n], g = pg[n], y = py[n];
	int ans = min(min(dp[r][g][y][0], dp[r][g][y][1]), dp[r][g][y][2]);
	if (ans >= INF) {
		cout << "-1\n";
		return 0;
	}
	assert(ans % 2 == 0);
	cout << ans / 2 << "\n";
    return 0;
}

/**
RRGYY
->
RYRGY
**/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5888 KB Output is correct
2 Correct 4 ms 5888 KB Output is correct
3 Correct 3 ms 6016 KB Output is correct
4 Correct 5 ms 5888 KB Output is correct
5 Correct 3 ms 5888 KB Output is correct
6 Correct 3 ms 5888 KB Output is correct
7 Correct 3 ms 5888 KB Output is correct
8 Correct 3 ms 5888 KB Output is correct
9 Correct 3 ms 5888 KB Output is correct
10 Correct 4 ms 5888 KB Output is correct
11 Correct 3 ms 5888 KB Output is correct
12 Correct 4 ms 5888 KB Output is correct
13 Correct 3 ms 5888 KB Output is correct
14 Correct 3 ms 5888 KB Output is correct
15 Correct 3 ms 5888 KB Output is correct
16 Correct 4 ms 5888 KB Output is correct
17 Correct 3 ms 5888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5888 KB Output is correct
2 Correct 4 ms 5888 KB Output is correct
3 Correct 3 ms 6016 KB Output is correct
4 Correct 5 ms 5888 KB Output is correct
5 Correct 3 ms 5888 KB Output is correct
6 Correct 3 ms 5888 KB Output is correct
7 Correct 3 ms 5888 KB Output is correct
8 Correct 3 ms 5888 KB Output is correct
9 Correct 3 ms 5888 KB Output is correct
10 Correct 4 ms 5888 KB Output is correct
11 Correct 3 ms 5888 KB Output is correct
12 Correct 4 ms 5888 KB Output is correct
13 Correct 3 ms 5888 KB Output is correct
14 Correct 3 ms 5888 KB Output is correct
15 Correct 3 ms 5888 KB Output is correct
16 Correct 4 ms 5888 KB Output is correct
17 Correct 3 ms 5888 KB Output is correct
18 Correct 4 ms 5888 KB Output is correct
19 Correct 4 ms 5888 KB Output is correct
20 Correct 4 ms 5888 KB Output is correct
21 Correct 3 ms 5888 KB Output is correct
22 Correct 3 ms 5888 KB Output is correct
23 Correct 4 ms 5888 KB Output is correct
24 Correct 4 ms 5888 KB Output is correct
25 Correct 3 ms 5888 KB Output is correct
26 Correct 3 ms 5888 KB Output is correct
27 Correct 4 ms 5888 KB Output is correct
28 Correct 4 ms 5888 KB Output is correct
29 Correct 4 ms 5888 KB Output is correct
30 Correct 4 ms 5888 KB Output is correct
31 Correct 4 ms 5888 KB Output is correct
32 Correct 3 ms 5888 KB Output is correct
33 Correct 4 ms 5888 KB Output is correct
34 Correct 4 ms 5888 KB Output is correct
35 Correct 5 ms 5888 KB Output is correct
36 Correct 3 ms 5888 KB Output is correct
37 Correct 3 ms 5888 KB Output is correct
38 Correct 3 ms 5888 KB Output is correct
39 Correct 3 ms 5888 KB Output is correct
40 Correct 3 ms 5888 KB Output is correct
41 Correct 3 ms 5888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5888 KB Output is correct
2 Runtime error 11 ms 11776 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5888 KB Output is correct
2 Correct 4 ms 5888 KB Output is correct
3 Correct 3 ms 6016 KB Output is correct
4 Correct 5 ms 5888 KB Output is correct
5 Correct 3 ms 5888 KB Output is correct
6 Correct 3 ms 5888 KB Output is correct
7 Correct 3 ms 5888 KB Output is correct
8 Correct 3 ms 5888 KB Output is correct
9 Correct 3 ms 5888 KB Output is correct
10 Correct 4 ms 5888 KB Output is correct
11 Correct 3 ms 5888 KB Output is correct
12 Correct 4 ms 5888 KB Output is correct
13 Correct 3 ms 5888 KB Output is correct
14 Correct 3 ms 5888 KB Output is correct
15 Correct 3 ms 5888 KB Output is correct
16 Correct 4 ms 5888 KB Output is correct
17 Correct 3 ms 5888 KB Output is correct
18 Correct 4 ms 5888 KB Output is correct
19 Correct 4 ms 5888 KB Output is correct
20 Correct 4 ms 5888 KB Output is correct
21 Correct 3 ms 5888 KB Output is correct
22 Correct 3 ms 5888 KB Output is correct
23 Correct 4 ms 5888 KB Output is correct
24 Correct 4 ms 5888 KB Output is correct
25 Correct 3 ms 5888 KB Output is correct
26 Correct 3 ms 5888 KB Output is correct
27 Correct 4 ms 5888 KB Output is correct
28 Correct 4 ms 5888 KB Output is correct
29 Correct 4 ms 5888 KB Output is correct
30 Correct 4 ms 5888 KB Output is correct
31 Correct 4 ms 5888 KB Output is correct
32 Correct 3 ms 5888 KB Output is correct
33 Correct 4 ms 5888 KB Output is correct
34 Correct 4 ms 5888 KB Output is correct
35 Correct 5 ms 5888 KB Output is correct
36 Correct 3 ms 5888 KB Output is correct
37 Correct 3 ms 5888 KB Output is correct
38 Correct 3 ms 5888 KB Output is correct
39 Correct 3 ms 5888 KB Output is correct
40 Correct 3 ms 5888 KB Output is correct
41 Correct 3 ms 5888 KB Output is correct
42 Correct 4 ms 5888 KB Output is correct
43 Runtime error 11 ms 11776 KB Execution killed with signal 11 (could be triggered by violating memory limits)
44 Halted 0 ms 0 KB -