답안 #376976

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376976 2021-03-12T13:49:26 Z peijar Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
60 / 100
500 ms 1009772 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 401;
const int INF = 1e9;

int dp[MAXN][MAXN][MAXN][4];

signed main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	int nbFleurs;
	string fleurs;
	cin >> nbFleurs >> fleurs;
	vector<vector<int>> positions(3);
	vector<vector<int>> prefixSum(3, vector<int>(nbFleurs+1));
	for (int i(0); i < nbFleurs; ++i)
	{
		for (int j(0); j < 3; ++j)
			prefixSum[j][i+1] = prefixSum[j][i];
		if (fleurs[i] == 'R')
		{
			positions[0].push_back(i);
			prefixSum[0][i+1]++;
		}
		else if (fleurs[i] == 'G')
		{
			positions[1].push_back(i);
			prefixSum[1][i+1]++;
	}
		else
		{
			positions[2].push_back(i);
			prefixSum[2][i+1]++;
		}
	}
	for (int nbRouge(0); nbRouge <= nbFleurs; ++nbRouge)
		for (int nbVerts(0); nbVerts <= nbFleurs; ++nbVerts)
			for (int nbJaunes(0); nbJaunes <= nbFleurs; ++nbJaunes)
				for (int dernier(0); dernier < 4; ++dernier)
					dp[nbRouge][nbVerts][nbJaunes][dernier]= INF;
	int rep = INF;
	int totRouges = (int)positions[0].size();
	int totVerts = (int)positions[1].size();
	int totJaunes = (int)positions[2].size();
	assert(totRouges >= 0 and totVerts >= 0 and totJaunes >= 0);
	dp[0][0][0][3] = 0;
	for (int nbRouge(0); nbRouge <= totRouges; ++nbRouge)
		for (int nbVerts(0); nbVerts <= totVerts; ++nbVerts)
			for (int nbJaunes(0); nbJaunes <= totJaunes ; ++nbJaunes)
				for (int dernier(0); dernier < 4; ++dernier)
				{
					int prix = dp[nbRouge][nbVerts][nbJaunes][dernier];
					if (prix == INF) continue;
					int curPos[3] = {nbRouge, nbVerts, nbJaunes};
					for (int prochain(0); prochain < 3; ++prochain)
					{
						if (curPos[prochain] >= (int)positions[prochain].size())
							continue;
						if (prochain == dernier) continue;
						int nbSwap(0);
						for (int autres(0); autres < 3; ++autres)
							if (autres != prochain and curPos[autres] < (int)positions[autres].size())
							{
								int posNxt = positions[prochain][curPos[prochain]];
								int posAutres = positions[autres][curPos[autres]];
								if (posNxt > posAutres)
									nbSwap += prefixSum[autres][posNxt+1] - prefixSum[autres][posAutres];
							}
						int nxtRouges = nbRouge, nxtVerts = nbVerts, nxtJaunes = nbJaunes;
						if (!prochain) nxtRouges++;
						else if (prochain == 1) nxtVerts++;
						else nxtJaunes++;
						dp[nxtRouges][nxtVerts][nxtJaunes][prochain] = 
						min(dp[nxtRouges][nxtVerts][nxtJaunes][prochain], nbSwap + prix);
					}
				}
	for (int dernier(0); dernier < 3; ++dernier)
		rep = min(rep, dp[totRouges][totVerts][totJaunes][dernier]);

	if (rep == INF)
		rep = -1;
	cout << rep << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 876 KB Output is correct
5 Correct 2 ms 1536 KB Output is correct
6 Correct 1 ms 1516 KB Output is correct
7 Correct 1 ms 1516 KB Output is correct
8 Correct 1 ms 1536 KB Output is correct
9 Correct 1 ms 1516 KB Output is correct
10 Correct 2 ms 1516 KB Output is correct
11 Correct 1 ms 1516 KB Output is correct
12 Correct 1 ms 1516 KB Output is correct
13 Correct 1 ms 1516 KB Output is correct
14 Correct 1 ms 1388 KB Output is correct
15 Correct 1 ms 1516 KB Output is correct
16 Correct 2 ms 1516 KB Output is correct
17 Correct 1 ms 1388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 876 KB Output is correct
5 Correct 2 ms 1536 KB Output is correct
6 Correct 1 ms 1516 KB Output is correct
7 Correct 1 ms 1516 KB Output is correct
8 Correct 1 ms 1536 KB Output is correct
9 Correct 1 ms 1516 KB Output is correct
10 Correct 2 ms 1516 KB Output is correct
11 Correct 1 ms 1516 KB Output is correct
12 Correct 1 ms 1516 KB Output is correct
13 Correct 1 ms 1516 KB Output is correct
14 Correct 1 ms 1388 KB Output is correct
15 Correct 1 ms 1516 KB Output is correct
16 Correct 2 ms 1516 KB Output is correct
17 Correct 1 ms 1388 KB Output is correct
18 Correct 11 ms 19052 KB Output is correct
19 Correct 12 ms 19052 KB Output is correct
20 Correct 11 ms 19052 KB Output is correct
21 Correct 11 ms 19052 KB Output is correct
22 Correct 11 ms 19052 KB Output is correct
23 Correct 11 ms 19052 KB Output is correct
24 Correct 11 ms 19052 KB Output is correct
25 Correct 12 ms 19052 KB Output is correct
26 Correct 12 ms 19052 KB Output is correct
27 Correct 11 ms 19052 KB Output is correct
28 Correct 11 ms 19052 KB Output is correct
29 Correct 11 ms 19052 KB Output is correct
30 Correct 11 ms 19052 KB Output is correct
31 Correct 11 ms 19052 KB Output is correct
32 Correct 11 ms 19052 KB Output is correct
33 Correct 11 ms 18284 KB Output is correct
34 Correct 11 ms 18284 KB Output is correct
35 Correct 10 ms 17660 KB Output is correct
36 Correct 10 ms 18284 KB Output is correct
37 Correct 10 ms 17004 KB Output is correct
38 Correct 11 ms 19052 KB Output is correct
39 Correct 11 ms 19052 KB Output is correct
40 Correct 10 ms 18284 KB Output is correct
41 Correct 11 ms 19052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Execution timed out 622 ms 1009772 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 876 KB Output is correct
5 Correct 2 ms 1536 KB Output is correct
6 Correct 1 ms 1516 KB Output is correct
7 Correct 1 ms 1516 KB Output is correct
8 Correct 1 ms 1536 KB Output is correct
9 Correct 1 ms 1516 KB Output is correct
10 Correct 2 ms 1516 KB Output is correct
11 Correct 1 ms 1516 KB Output is correct
12 Correct 1 ms 1516 KB Output is correct
13 Correct 1 ms 1516 KB Output is correct
14 Correct 1 ms 1388 KB Output is correct
15 Correct 1 ms 1516 KB Output is correct
16 Correct 2 ms 1516 KB Output is correct
17 Correct 1 ms 1388 KB Output is correct
18 Correct 11 ms 19052 KB Output is correct
19 Correct 12 ms 19052 KB Output is correct
20 Correct 11 ms 19052 KB Output is correct
21 Correct 11 ms 19052 KB Output is correct
22 Correct 11 ms 19052 KB Output is correct
23 Correct 11 ms 19052 KB Output is correct
24 Correct 11 ms 19052 KB Output is correct
25 Correct 12 ms 19052 KB Output is correct
26 Correct 12 ms 19052 KB Output is correct
27 Correct 11 ms 19052 KB Output is correct
28 Correct 11 ms 19052 KB Output is correct
29 Correct 11 ms 19052 KB Output is correct
30 Correct 11 ms 19052 KB Output is correct
31 Correct 11 ms 19052 KB Output is correct
32 Correct 11 ms 19052 KB Output is correct
33 Correct 11 ms 18284 KB Output is correct
34 Correct 11 ms 18284 KB Output is correct
35 Correct 10 ms 17660 KB Output is correct
36 Correct 10 ms 18284 KB Output is correct
37 Correct 10 ms 17004 KB Output is correct
38 Correct 11 ms 19052 KB Output is correct
39 Correct 11 ms 19052 KB Output is correct
40 Correct 10 ms 18284 KB Output is correct
41 Correct 11 ms 19052 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Execution timed out 622 ms 1009772 KB Time limit exceeded
44 Halted 0 ms 0 KB -