답안 #121212

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
121212 2019-06-26T08:10:59 Z 윤교준(#2970) Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
0 / 100
6 ms 4224 KB
#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define upmin(a,b) (a)=min((a),(b))
#define INF (0x3f3f3f3f)
using namespace std;

const int MAXN = 405;

int DP[2][MAXN][MAXN][3];

vector<int> AV[3];
int AC[MAXN][3];
int A[MAXN];

int N, Ans = INF;

int f(int a, int b, int c, int v) {
	if(!v) v = AV[0][a];
	else if(1 == v) v = AV[1][b];
	else v = AV[2][c];
	return max(0, AC[v-1][0]-a) + max(0, AC[v-1][1]-b) + max(0, AC[v-1][2]-c);
}

int main() {
	{
		char str[MAXN];
		scanf("%d %s", &N, str+1);
		for(int i = 1; i <= N; i++) {
			if('R' == str[i]) A[i] = 1;
			if('G' == str[i]) A[i] = 2;
		}
	}
	for(int i = 1; i <= N; i++) AV[A[i]].eb(i);
	for(int i = 1; i <= N; i++) {
		for(int t = 0; t < 3; t++) {
			AC[i][t] = AC[i-1][t];
		}
		AC[i][A[i]]++;
	}

	fill(DP[0][0][0], DP[1][MAXN-1][MAXN-1]+3, INF);

	if(!AV[0].empty()) DP[1][1][0][0] = AV[0][0] - 1;
	if(!AV[1].empty()) DP[1][0][1][1] = AV[1][0] - 1;
	if(!AV[2].empty()) DP[1][0][0][2] = AV[2][0] - 1;

	for(int l = 1; l < N; l++) {
		for(int a = 0; a <= l && a <= sz(AV[0]); a++) {
			for(int b = 0; b <= l && b <= sz(AV[1]); b++) {
				int c = l-a-b;
				if(c < 0 || sz(AV[2]) < c) continue;
				for(int t = 0; t < 3; t++) {
					int ret = DP[l&1][a][b][t];
					if(INF <= ret) continue;

					for(int v = 0; v < 3; v++) {
						if(t == v) continue;
						if(!v) {
							if(a == sz(AV[0])) continue;
							upmin(DP[1^l&1][a+1][b][0], ret + f(a, b, c, 0));
						} else if(1 == v) {
							if(b == sz(AV[1])) continue;
							upmin(DP[1^l&1][a][b+1][1], ret + f(a, b, c, 1));
						} else {
							if(c == sz(AV[2])) continue;
							upmin(DP[1^l&1][a][b][2], ret + f(a, b, c, 2));
						}
					}
				}
			}
		}
	}

	for(int i = 0; i < 3; i++)
		upmin(Ans, DP[N&1][sz(AV[0])][sz(AV[1])][i]);
	
	cout << (INF <= Ans ? -1 : Ans) << endl;
	return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:61:20: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
        upmin(DP[1^l&1][a+1][b][0], ret + f(a, b, c, 0));
                   ~^~
joi2019_ho_t3.cpp:4:21: note: in definition of macro 'upmin'
 #define upmin(a,b) (a)=min((a),(b))
                     ^
joi2019_ho_t3.cpp:61:20: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
        upmin(DP[1^l&1][a+1][b][0], ret + f(a, b, c, 0));
                   ~^~
joi2019_ho_t3.cpp:4:29: note: in definition of macro 'upmin'
 #define upmin(a,b) (a)=min((a),(b))
                             ^
joi2019_ho_t3.cpp:64:20: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
        upmin(DP[1^l&1][a][b+1][1], ret + f(a, b, c, 1));
                   ~^~
joi2019_ho_t3.cpp:4:21: note: in definition of macro 'upmin'
 #define upmin(a,b) (a)=min((a),(b))
                     ^
joi2019_ho_t3.cpp:64:20: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
        upmin(DP[1^l&1][a][b+1][1], ret + f(a, b, c, 1));
                   ~^~
joi2019_ho_t3.cpp:4:29: note: in definition of macro 'upmin'
 #define upmin(a,b) (a)=min((a),(b))
                             ^
joi2019_ho_t3.cpp:67:20: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
        upmin(DP[1^l&1][a][b][2], ret + f(a, b, c, 2));
                   ~^~
joi2019_ho_t3.cpp:4:21: note: in definition of macro 'upmin'
 #define upmin(a,b) (a)=min((a),(b))
                     ^
joi2019_ho_t3.cpp:67:20: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
        upmin(DP[1^l&1][a][b][2], ret + f(a, b, c, 2));
                   ~^~
joi2019_ho_t3.cpp:4:29: note: in definition of macro 'upmin'
 #define upmin(a,b) (a)=min((a),(b))
                             ^
joi2019_ho_t3.cpp:28:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %s", &N, str+1);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4224 KB Output is correct
2 Correct 5 ms 4224 KB Output is correct
3 Correct 5 ms 4096 KB Output is correct
4 Incorrect 5 ms 4224 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4224 KB Output is correct
2 Correct 5 ms 4224 KB Output is correct
3 Correct 5 ms 4096 KB Output is correct
4 Incorrect 5 ms 4224 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4224 KB Output is correct
2 Correct 5 ms 4224 KB Output is correct
3 Correct 6 ms 4224 KB Output is correct
4 Correct 5 ms 4224 KB Output is correct
5 Correct 6 ms 4224 KB Output is correct
6 Correct 6 ms 4224 KB Output is correct
7 Incorrect 6 ms 4224 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4224 KB Output is correct
2 Correct 5 ms 4224 KB Output is correct
3 Correct 5 ms 4096 KB Output is correct
4 Incorrect 5 ms 4224 KB Output isn't correct
5 Halted 0 ms 0 KB -