제출 #506219

#제출 시각아이디문제언어결과실행 시간메모리
506219sidonGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
60 / 100
219 ms196528 KiB
#include <bits/stdc++.h>
using namespace std;
#define minim(X, Y) (X = min(X, Y))
#define F(X, Y) for(int X = 0; X < Y; X++)
#define g(X) (X ? (X > 1 ? z : y) : x)

const int Z = 202, INF = 1e9;

int N, A[Z*2], DP[Z][Z][Z][3], p[3][Z*2], h[3][Z*2], c[3], x, y, z;

int main() {
	cin >> N, getchar();
	F(i, N) {
		char v = getchar();
		A[i] = (v < 82) + (v < 89);
	}

	F(i, Z) F(j, Z) F(k, Z) F(l, 3)
		DP[i][j][k][l] = (i || j || k) ? INF : 0;

	F(i, N) {
		if(++c[A[i]] >= Z-1) break;
		p[A[i]][g(A[i]) = c[A[i]]] = i;
		F(j, 3) h[j][i] = c[j];

		F(j, i+1) F(k, i+1) {
			(A[i] ? x : y) = j;
			(A[i] < 2 ? z : y) = k;
			F(o, 3) if(g(o) > c[o]) continue;
			F(o, 3) {
				int &w = g(o);
				if(!w) continue;
				int a = INF, b = 0; --w;
				F(r, 3) minim(a, r != o ? DP[x][y][z][r] : INF); ++w;
				F(r, 3) b += r != o ? max(0, g(r) - h[r][p[o][w]]) : 0;
				minim(DP[x][y][z][o], a + b);
			}
		}
	}
	int res = INF;
	F(i, 3) minim(res, DP[c[0]][c[1]][c[2]][i]);

	cout << (res == INF ? -1 : res);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...