Submission #252713

# Submission time Handle Problem Language Result Execution time Memory
252713 2020-07-26T07:19:06 Z Mlxa Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
60 / 100
358 ms 379000 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 uint16_t

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

const int N   = 401;
const int INF = SHRT_MAX;

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
**/
# Verdict Execution time Memory Grader output
1 Correct 327 ms 378872 KB Output is correct
2 Correct 314 ms 378956 KB Output is correct
3 Correct 311 ms 378872 KB Output is correct
4 Correct 318 ms 378872 KB Output is correct
5 Correct 311 ms 378876 KB Output is correct
6 Correct 315 ms 378944 KB Output is correct
7 Correct 321 ms 379000 KB Output is correct
8 Correct 318 ms 378908 KB Output is correct
9 Correct 316 ms 378872 KB Output is correct
10 Correct 306 ms 378872 KB Output is correct
11 Correct 337 ms 379000 KB Output is correct
12 Correct 313 ms 379000 KB Output is correct
13 Correct 331 ms 378872 KB Output is correct
14 Correct 326 ms 378936 KB Output is correct
15 Correct 358 ms 378956 KB Output is correct
16 Correct 344 ms 378872 KB Output is correct
17 Correct 338 ms 378980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 327 ms 378872 KB Output is correct
2 Correct 314 ms 378956 KB Output is correct
3 Correct 311 ms 378872 KB Output is correct
4 Correct 318 ms 378872 KB Output is correct
5 Correct 311 ms 378876 KB Output is correct
6 Correct 315 ms 378944 KB Output is correct
7 Correct 321 ms 379000 KB Output is correct
8 Correct 318 ms 378908 KB Output is correct
9 Correct 316 ms 378872 KB Output is correct
10 Correct 306 ms 378872 KB Output is correct
11 Correct 337 ms 379000 KB Output is correct
12 Correct 313 ms 379000 KB Output is correct
13 Correct 331 ms 378872 KB Output is correct
14 Correct 326 ms 378936 KB Output is correct
15 Correct 358 ms 378956 KB Output is correct
16 Correct 344 ms 378872 KB Output is correct
17 Correct 338 ms 378980 KB Output is correct
18 Correct 315 ms 378876 KB Output is correct
19 Correct 313 ms 378872 KB Output is correct
20 Correct 327 ms 379000 KB Output is correct
21 Correct 310 ms 378876 KB Output is correct
22 Correct 315 ms 379000 KB Output is correct
23 Correct 312 ms 378872 KB Output is correct
24 Correct 324 ms 378872 KB Output is correct
25 Correct 315 ms 378872 KB Output is correct
26 Correct 316 ms 378872 KB Output is correct
27 Correct 320 ms 378876 KB Output is correct
28 Correct 324 ms 378892 KB Output is correct
29 Correct 326 ms 378872 KB Output is correct
30 Correct 319 ms 378820 KB Output is correct
31 Correct 314 ms 378872 KB Output is correct
32 Correct 343 ms 378872 KB Output is correct
33 Correct 329 ms 378872 KB Output is correct
34 Correct 330 ms 379000 KB Output is correct
35 Correct 310 ms 378872 KB Output is correct
36 Correct 325 ms 378952 KB Output is correct
37 Correct 315 ms 378908 KB Output is correct
38 Correct 318 ms 379000 KB Output is correct
39 Correct 320 ms 378876 KB Output is correct
40 Correct 329 ms 378876 KB Output is correct
41 Correct 319 ms 378872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 319 ms 378876 KB Output is correct
2 Incorrect 343 ms 379000 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 327 ms 378872 KB Output is correct
2 Correct 314 ms 378956 KB Output is correct
3 Correct 311 ms 378872 KB Output is correct
4 Correct 318 ms 378872 KB Output is correct
5 Correct 311 ms 378876 KB Output is correct
6 Correct 315 ms 378944 KB Output is correct
7 Correct 321 ms 379000 KB Output is correct
8 Correct 318 ms 378908 KB Output is correct
9 Correct 316 ms 378872 KB Output is correct
10 Correct 306 ms 378872 KB Output is correct
11 Correct 337 ms 379000 KB Output is correct
12 Correct 313 ms 379000 KB Output is correct
13 Correct 331 ms 378872 KB Output is correct
14 Correct 326 ms 378936 KB Output is correct
15 Correct 358 ms 378956 KB Output is correct
16 Correct 344 ms 378872 KB Output is correct
17 Correct 338 ms 378980 KB Output is correct
18 Correct 315 ms 378876 KB Output is correct
19 Correct 313 ms 378872 KB Output is correct
20 Correct 327 ms 379000 KB Output is correct
21 Correct 310 ms 378876 KB Output is correct
22 Correct 315 ms 379000 KB Output is correct
23 Correct 312 ms 378872 KB Output is correct
24 Correct 324 ms 378872 KB Output is correct
25 Correct 315 ms 378872 KB Output is correct
26 Correct 316 ms 378872 KB Output is correct
27 Correct 320 ms 378876 KB Output is correct
28 Correct 324 ms 378892 KB Output is correct
29 Correct 326 ms 378872 KB Output is correct
30 Correct 319 ms 378820 KB Output is correct
31 Correct 314 ms 378872 KB Output is correct
32 Correct 343 ms 378872 KB Output is correct
33 Correct 329 ms 378872 KB Output is correct
34 Correct 330 ms 379000 KB Output is correct
35 Correct 310 ms 378872 KB Output is correct
36 Correct 325 ms 378952 KB Output is correct
37 Correct 315 ms 378908 KB Output is correct
38 Correct 318 ms 379000 KB Output is correct
39 Correct 320 ms 378876 KB Output is correct
40 Correct 329 ms 378876 KB Output is correct
41 Correct 319 ms 378872 KB Output is correct
42 Correct 319 ms 378876 KB Output is correct
43 Incorrect 343 ms 379000 KB Output isn't correct
44 Halted 0 ms 0 KB -