Submission #376986

# Submission time Handle Problem Language Result Execution time Memory
376986 2021-03-12T14:42:59 Z peijar Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
75 / 100
500 ms 757612 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;

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

int dp[MAXN][MAXN][MAXN][3];

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)
	{
		if (fleurs[i] == 'R')
			positions[0].push_back(i);
		else if (fleurs[i] == 'G')
			positions[1].push_back(i);
		else
			positions[2].push_back(i);
		for (int j(0); j < 3; ++j)
			prefixSum[j][i+1] = (int)positions[j].size();
	}
	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 < 3; ++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();
	if (totRouges)
		dp[1][0][0][0] = positions[0][0];
	if (totVerts)
		dp[0][1][0][1] = positions[1][0];
	if (totJaunes)
		dp[0][0][1][2] = positions[2][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 < 3; ++dernier)
					if (dp[nbRouge][nbVerts][nbJaunes][dernier] < INF)
					{
						int prix = dp[nbRouge][nbVerts][nbJaunes][dernier];
						//cout << prix << ' ' << dernier << endl;
						// On met un rouge : 
						if (nbRouge < totRouges and dernier)
						{
							int nbSwaps = 0;
							if (nbVerts < totVerts and positions[0][nbRouge] > positions[1][nbVerts])
								nbSwaps += prefixSum[1][positions[0][nbRouge]]
									 - prefixSum[1][positions[1][nbVerts]];
							if (nbJaunes < totJaunes and positions[0][nbRouge] > positions[2][nbJaunes])
								nbSwaps += prefixSum[2][positions[0][nbRouge]]
									- prefixSum[2][positions[2][nbJaunes]];
							dp[nbRouge+1][nbVerts][nbJaunes][0]
								= min(dp[nbRouge+1][nbVerts][nbJaunes][0], prix+nbSwaps);
						}
						// On met un vert :
						if (nbVerts < totVerts and dernier != 1)
						{
							int nbSwaps = 0;
							if (nbRouge < totRouges and positions[1][nbVerts] > positions[0][nbRouge])
								nbSwaps += prefixSum[0][positions[1][nbVerts]]
									 - prefixSum[0][positions[0][nbRouge]];
							if (nbJaunes < totJaunes and positions[1][nbVerts] > positions[2][nbJaunes])
								nbSwaps += prefixSum[2][positions[1][nbVerts]]
									- prefixSum[2][positions[2][nbJaunes]];
							dp[nbRouge][nbVerts+1][nbJaunes][1]
								= min(dp[nbRouge][nbVerts+1][nbJaunes][1], prix+nbSwaps);
						}
						if (nbJaunes < totJaunes and dernier != 2)
						{
							int nbSwaps = 0;
							if (nbRouge < totRouges and positions[2][nbJaunes] > positions[0][nbRouge])
								nbSwaps += prefixSum[0][positions[2][nbJaunes]]
									 - prefixSum[0][positions[0][nbRouge]];
							if (nbVerts < totVerts and positions[2][nbJaunes] > positions[1][nbVerts])
								nbSwaps += prefixSum[1][positions[2][nbJaunes]]
									- prefixSum[1][positions[1][nbVerts]];
							dp[nbRouge][nbVerts][nbJaunes+1][2]
								= min(dp[nbRouge][nbVerts][nbJaunes+1][2], prix+nbSwaps);
						}
					}
	for (int dernier(0); dernier < 3; ++dernier)
		rep = min(rep, dp[totRouges][totVerts][totJaunes][dernier]);

	if (rep == INF)
		rep = -1;
	cout << rep << endl;
}
# Verdict Execution time Memory 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 1 ms 1516 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 1516 KB Output is correct
9 Correct 2 ms 1516 KB Output is correct
10 Correct 1 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 2 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 1 ms 1516 KB Output is correct
17 Correct 1 ms 1388 KB Output is correct
# Verdict Execution time Memory 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 1 ms 1516 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 1516 KB Output is correct
9 Correct 2 ms 1516 KB Output is correct
10 Correct 1 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 2 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 1 ms 1516 KB Output is correct
17 Correct 1 ms 1388 KB Output is correct
18 Correct 11 ms 18028 KB Output is correct
19 Correct 10 ms 18028 KB Output is correct
20 Correct 10 ms 18028 KB Output is correct
21 Correct 10 ms 18028 KB Output is correct
22 Correct 13 ms 18176 KB Output is correct
23 Correct 12 ms 18028 KB Output is correct
24 Correct 12 ms 18172 KB Output is correct
25 Correct 10 ms 18028 KB Output is correct
26 Correct 10 ms 18028 KB Output is correct
27 Correct 10 ms 18028 KB Output is correct
28 Correct 10 ms 18028 KB Output is correct
29 Correct 10 ms 18028 KB Output is correct
30 Correct 11 ms 18028 KB Output is correct
31 Correct 11 ms 18028 KB Output is correct
32 Correct 10 ms 18028 KB Output is correct
33 Correct 10 ms 17652 KB Output is correct
34 Correct 10 ms 17516 KB Output is correct
35 Correct 11 ms 16876 KB Output is correct
36 Correct 10 ms 17516 KB Output is correct
37 Correct 9 ms 16236 KB Output is correct
38 Correct 10 ms 18028 KB Output is correct
39 Correct 11 ms 18028 KB Output is correct
40 Correct 9 ms 17516 KB Output is correct
41 Correct 10 ms 18028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 409 ms 757532 KB Output is correct
3 Correct 412 ms 755308 KB Output is correct
4 Correct 406 ms 757484 KB Output is correct
5 Correct 408 ms 757484 KB Output is correct
6 Correct 409 ms 757484 KB Output is correct
7 Correct 406 ms 755308 KB Output is correct
8 Correct 406 ms 755436 KB Output is correct
9 Correct 406 ms 751468 KB Output is correct
10 Correct 406 ms 757612 KB Output is correct
11 Correct 423 ms 757612 KB Output is correct
12 Correct 110 ms 202860 KB Output is correct
13 Correct 196 ms 357484 KB Output is correct
14 Correct 280 ms 517484 KB Output is correct
15 Correct 420 ms 757564 KB Output is correct
16 Correct 422 ms 757452 KB Output is correct
# Verdict Execution time Memory 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 1 ms 1516 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 1516 KB Output is correct
9 Correct 2 ms 1516 KB Output is correct
10 Correct 1 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 2 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 1 ms 1516 KB Output is correct
17 Correct 1 ms 1388 KB Output is correct
18 Correct 11 ms 18028 KB Output is correct
19 Correct 10 ms 18028 KB Output is correct
20 Correct 10 ms 18028 KB Output is correct
21 Correct 10 ms 18028 KB Output is correct
22 Correct 13 ms 18176 KB Output is correct
23 Correct 12 ms 18028 KB Output is correct
24 Correct 12 ms 18172 KB Output is correct
25 Correct 10 ms 18028 KB Output is correct
26 Correct 10 ms 18028 KB Output is correct
27 Correct 10 ms 18028 KB Output is correct
28 Correct 10 ms 18028 KB Output is correct
29 Correct 10 ms 18028 KB Output is correct
30 Correct 11 ms 18028 KB Output is correct
31 Correct 11 ms 18028 KB Output is correct
32 Correct 10 ms 18028 KB Output is correct
33 Correct 10 ms 17652 KB Output is correct
34 Correct 10 ms 17516 KB Output is correct
35 Correct 11 ms 16876 KB Output is correct
36 Correct 10 ms 17516 KB Output is correct
37 Correct 9 ms 16236 KB Output is correct
38 Correct 10 ms 18028 KB Output is correct
39 Correct 11 ms 18028 KB Output is correct
40 Correct 9 ms 17516 KB Output is correct
41 Correct 10 ms 18028 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 409 ms 757532 KB Output is correct
44 Correct 412 ms 755308 KB Output is correct
45 Correct 406 ms 757484 KB Output is correct
46 Correct 408 ms 757484 KB Output is correct
47 Correct 409 ms 757484 KB Output is correct
48 Correct 406 ms 755308 KB Output is correct
49 Correct 406 ms 755436 KB Output is correct
50 Correct 406 ms 751468 KB Output is correct
51 Correct 406 ms 757612 KB Output is correct
52 Correct 423 ms 757612 KB Output is correct
53 Correct 110 ms 202860 KB Output is correct
54 Correct 196 ms 357484 KB Output is correct
55 Correct 280 ms 517484 KB Output is correct
56 Correct 420 ms 757564 KB Output is correct
57 Correct 422 ms 757452 KB Output is correct
58 Execution timed out 560 ms 757440 KB Time limit exceeded
59 Halted 0 ms 0 KB -