답안 #104947

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
104947 2019-04-09T22:26:53 Z tincamatei Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
15 / 100
124 ms 4216 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 400;
const int INF = 1000000000;

int dp[2][1+MAX_N][1+MAX_N][3];
vector<int> pos[3];

char str[1+MAX_N];

char getchar(FILE *fin) {
	char ch = fgetc(fin);
	while(!isalpha(ch))
		ch = fgetc(fin);
	return ch;
}

void initDp(int p) {
	for(int i = 0; i <= MAX_N; ++i)
		for(int j = 0; j <= MAX_N; ++j)
			for(int k = 0; k < 3; ++k)
				dp[p][i][j][k] = INF;
}

int main() {
#ifdef HOME
	FILE *fin = fopen("input.in", "r");
	FILE *fout = fopen("output.out", "w");
#else
	FILE *fin = stdin;
	FILE *fout = stdout;
#endif
	
	int n;
	fscanf(fin, "%d", &n);

	for(int i = 1; i <= n; ++i) {
		str[i] = getchar(fin);
		if(str[i] == 'R')
			str[i] = 2;
		else if(str[i] == 'G')
			str[i] = 0;
		else
			str[i] = 1;
		
		pos[str[i]].push_back(i);
	}
	
	initDp(0);
	dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
	for(int i = 1; i <= n; ++i) {
		int p = i % 2;
		int ind[3];
		initDp(p);
		
		for(ind[0] = 0; ind[0] <= pos[0].size(); ++ind[0])
			for(ind[1] = 0; ind[1] <= pos[1].size(); ++ind[1]) {
				ind[2] = i - ind[0] - ind[1];
				if(0 <= ind[2] && ind[2] <= pos[2].size()) {
					for(int oldblock = 0; oldblock < 3; ++oldblock)
						for(int newblock = 0; newblock < 3; ++newblock) {
							if(oldblock != newblock) {
								int indcp[3];
								for(int x = 0; x < 3; ++x)
									indcp[x] = ind[x];
								
								indcp[newblock]--;
								if(indcp[newblock] >= 0) {
									dp[p][ind[0]][ind[1]][newblock] = min(dp[p][ind[0]][ind[1]][newblock],
								  	                                    dp[1-p][indcp[0]][indcp[1]][oldblock] + abs(i - pos[newblock][indcp[newblock]]));
								}
							}
						}
				}
			}
	}
	
	int rez = min(min(dp[n % 2][pos[0].size()][pos[1].size()][0],
	                  dp[n % 2][pos[0].size()][pos[1].size()][1]),
	                  dp[n % 2][pos[0].size()][pos[1].size()][2]);
	
	if(rez >= INF)
		fprintf(fout, "-1");
	else
		fprintf(fout, "%d", rez / 2);

	fclose(fin);
	fclose(fout);
	return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:48:13: warning: array subscript has type 'char' [-Wchar-subscripts]
   pos[str[i]].push_back(i);
             ^
joi2019_ho_t3.cpp:58:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(ind[0] = 0; ind[0] <= pos[0].size(); ++ind[0])
                   ~~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:59:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(ind[1] = 0; ind[1] <= pos[1].size(); ++ind[1]) {
                    ~~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:61:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(0 <= ind[2] && ind[2] <= pos[2].size()) {
                       ~~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:37:8: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  fscanf(fin, "%d", &n);
  ~~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4096 KB Output is correct
2 Correct 5 ms 4096 KB Output is correct
3 Correct 5 ms 4096 KB Output is correct
4 Correct 6 ms 4096 KB Output is correct
5 Correct 8 ms 4096 KB Output is correct
6 Correct 8 ms 4096 KB Output is correct
7 Correct 9 ms 4096 KB Output is correct
8 Correct 8 ms 4096 KB Output is correct
9 Correct 8 ms 4096 KB Output is correct
10 Correct 8 ms 4096 KB Output is correct
11 Incorrect 8 ms 4096 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4096 KB Output is correct
2 Correct 5 ms 4096 KB Output is correct
3 Correct 5 ms 4096 KB Output is correct
4 Correct 6 ms 4096 KB Output is correct
5 Correct 8 ms 4096 KB Output is correct
6 Correct 8 ms 4096 KB Output is correct
7 Correct 9 ms 4096 KB Output is correct
8 Correct 8 ms 4096 KB Output is correct
9 Correct 8 ms 4096 KB Output is correct
10 Correct 8 ms 4096 KB Output is correct
11 Incorrect 8 ms 4096 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4096 KB Output is correct
2 Correct 96 ms 4096 KB Output is correct
3 Correct 102 ms 4136 KB Output is correct
4 Correct 101 ms 4096 KB Output is correct
5 Correct 91 ms 4216 KB Output is correct
6 Correct 106 ms 4148 KB Output is correct
7 Correct 90 ms 4096 KB Output is correct
8 Correct 122 ms 4096 KB Output is correct
9 Correct 93 ms 4096 KB Output is correct
10 Correct 104 ms 4188 KB Output is correct
11 Correct 100 ms 4096 KB Output is correct
12 Correct 52 ms 4132 KB Output is correct
13 Correct 62 ms 4096 KB Output is correct
14 Correct 82 ms 4096 KB Output is correct
15 Correct 109 ms 4096 KB Output is correct
16 Correct 124 ms 4096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4096 KB Output is correct
2 Correct 5 ms 4096 KB Output is correct
3 Correct 5 ms 4096 KB Output is correct
4 Correct 6 ms 4096 KB Output is correct
5 Correct 8 ms 4096 KB Output is correct
6 Correct 8 ms 4096 KB Output is correct
7 Correct 9 ms 4096 KB Output is correct
8 Correct 8 ms 4096 KB Output is correct
9 Correct 8 ms 4096 KB Output is correct
10 Correct 8 ms 4096 KB Output is correct
11 Incorrect 8 ms 4096 KB Output isn't correct
12 Halted 0 ms 0 KB -