Submission #142743

# Submission time Handle Problem Language Result Execution time Memory
142743 2019-08-10T16:13:35 Z DrumpfTheGodEmperor Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
415 ms 196212 KB
#include <bits/stdc++.h>
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
		freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 405;
int n, dp[N][405][N][3], cntPrefix[N][3];
string s;
vector<int> reds, greens, yellows;
int getRealPosition(int ogPos, int prvRed, int prvGreen, int prvYellow) {
	//prvRed - cntPrefix[ogPos][0] is the number of reds that are moved to before ogPos
	int ans = 0;
	ans += max(0, prvRed - cntPrefix[ogPos][0]);
	ans += max(0, prvGreen - cntPrefix[ogPos][1]);
	ans += max(0, prvYellow - cntPrefix[ogPos][2]);
	return ogPos + ans + 1; //Difference between array indices and dp indices.
}
signed main() {
	#ifdef BLU
	in("blu.inp");
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> s;
	fw (i, 0, s.length()) {
		if (s[i] == 'R') reds.pb(i);
		else if (s[i] == 'G') greens.pb(i);
		else yellows.pb(i);
	}
	//0: red, 1: green, 2: yellow
	fw (i, 0, s.length()) {
		fw (color, 0, 3) cntPrefix[i][color] = (i ? cntPrefix[i - 1][color] : 0);
		if (s[i] == 'R') cntPrefix[i][0]++;
		else if (s[i] == 'G') cntPrefix[i][1]++;
		else cntPrefix[i][2]++;
	}
	fw (r, 0, n + 1) fw (g, 0, n + 1) fw (color, 0, 3) dp[1][r][g][color] = inf;
	//dp(i, r, g, color): Minimal cost for the first i items, j reds and k greens, i - j - k yellows. The
	//last leave is colored "color".
	if (reds.size() > 0) dp[1][1][0][0] = reds[0];
	if (greens.size() > 0) dp[1][0][1][1] = greens[0];
	if (yellows.size() > 0) dp[1][0][0][2] = yellows[0];
	fw (i, 2, n + 1) fw (r, 0, min((i + 1) / 2, (int)reds.size()) + 1) fw (g, 0, min(i - r, (int)greens.size()) + 1)
																					fw (color, 0, 3) {
//		cout << "dp[" << i << "][" << r << "][" << g << "][" << color << "]\n";
		dp[i][r][g][color] = inf;
		if (color == 0 && r == 0) continue;
		if (color == 1 && g == 0) continue;
		if (color == 2 && i - r - g == 0) continue;
		fw (prvColor, 0, 3) if (prvColor != color) {
			int newr = r, newg = g;
			if (color == 0) newr--;
			if (color == 1) newg--;
			if (newr < 0 || newg < 0) continue;
			if (i - 1 - newr - newg < 0) continue;
			if (newr > i / 2) continue;
//			cout << "prv state = " << i - 1 << " " << newr << " " << newg << " " << prvColor << " dp = " << dp[i - 1][newr][newg][prvColor] << "\n";
			dp[i][r][g][color] = min(dp[i][r][g][color], dp[i - 1][newr][newg][prvColor]);
		}
		if (dp[i][r][g][color] == inf) continue; //Non - valid state
		int y = i - r - g;
		if (y > yellows.size()) continue;
		if (color == 0) {
			dp[i][r][g][color] += getRealPosition(reds[r - 1], r - 1, g, y) - i;
		} else if (color == 1) {
			dp[i][r][g][color] += getRealPosition(greens[g - 1], r, g - 1, y) - i;
		} else {
			dp[i][r][g][color] += getRealPosition(yellows[y - 1], r, g, y - 1) - i;
		}
	}
//	fw (i, 1, n + 1) fw (r, 0, n + 1) fw (g, 0, n + 1) fw (color, 0, 3) {
//		cout << "dp[" << i << "][" << r << "][" << g << "][" << color << "] = " << dp[i][r][g][color] << "\n";
//	}
	if (reds.size() > (n + 1) / 2) {
		cout << "-1";
		return 0;
	}
	if (greens.size() > (n + 1) / 2) {
		cout << "-1";
		return 0;
	}
	if (yellows.size() > (n + 1) / 2) {
		cout << "-1";
		return 0;
	}
	int ans = inf;
	fw (color, 0, 3) ans = min(ans, dp[n][reds.size()][greens.size()][color]);
	if (ans == inf) cout << "-1";
	else cout << ans;
	return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
joi2019_ho_t3.cpp:34:6:
  fw (i, 0, s.length()) {
      ~~~~~~~~~~~~~~~~                  
joi2019_ho_t3.cpp:34:2: note: in expansion of macro 'fw'
  fw (i, 0, s.length()) {
  ^~
joi2019_ho_t3.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
joi2019_ho_t3.cpp:40:6:
  fw (i, 0, s.length()) {
      ~~~~~~~~~~~~~~~~                  
joi2019_ho_t3.cpp:40:2: note: in expansion of macro 'fw'
  fw (i, 0, s.length()) {
  ^~
joi2019_ho_t3.cpp:71:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (y > yellows.size()) continue;
       ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:83:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (reds.size() > (n + 1) / 2) {
      ~~~~~~~~~~~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:87:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (greens.size() > (n + 1) / 2) {
      ~~~~~~~~~~~~~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:91:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (yellows.size() > (n + 1) / 2) {
      ~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 760 KB Output is correct
6 Correct 2 ms 760 KB Output is correct
7 Correct 2 ms 760 KB Output is correct
8 Correct 2 ms 760 KB Output is correct
9 Correct 2 ms 760 KB Output is correct
10 Correct 2 ms 756 KB Output is correct
11 Correct 2 ms 760 KB Output is correct
12 Correct 2 ms 760 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 2 ms 632 KB Output is correct
15 Correct 2 ms 696 KB Output is correct
16 Correct 2 ms 760 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 760 KB Output is correct
6 Correct 2 ms 760 KB Output is correct
7 Correct 2 ms 760 KB Output is correct
8 Correct 2 ms 760 KB Output is correct
9 Correct 2 ms 760 KB Output is correct
10 Correct 2 ms 756 KB Output is correct
11 Correct 2 ms 760 KB Output is correct
12 Correct 2 ms 760 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 2 ms 632 KB Output is correct
15 Correct 2 ms 696 KB Output is correct
16 Correct 2 ms 760 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 7 ms 4820 KB Output is correct
19 Correct 4 ms 4444 KB Output is correct
20 Correct 7 ms 4856 KB Output is correct
21 Correct 7 ms 4688 KB Output is correct
22 Correct 6 ms 4728 KB Output is correct
23 Correct 6 ms 4344 KB Output is correct
24 Correct 5 ms 3320 KB Output is correct
25 Correct 6 ms 5112 KB Output is correct
26 Correct 6 ms 5112 KB Output is correct
27 Correct 6 ms 4984 KB Output is correct
28 Correct 6 ms 4728 KB Output is correct
29 Correct 6 ms 4732 KB Output is correct
30 Correct 5 ms 4216 KB Output is correct
31 Correct 6 ms 4088 KB Output is correct
32 Correct 6 ms 4344 KB Output is correct
33 Correct 8 ms 4856 KB Output is correct
34 Correct 6 ms 4728 KB Output is correct
35 Correct 7 ms 4600 KB Output is correct
36 Correct 7 ms 4472 KB Output is correct
37 Correct 5 ms 3576 KB Output is correct
38 Correct 5 ms 3960 KB Output is correct
39 Correct 6 ms 4472 KB Output is correct
40 Correct 2 ms 1012 KB Output is correct
41 Correct 5 ms 3192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 395 ms 196204 KB Output is correct
3 Correct 415 ms 195192 KB Output is correct
4 Correct 387 ms 196212 KB Output is correct
5 Correct 383 ms 196184 KB Output is correct
6 Correct 396 ms 196064 KB Output is correct
7 Correct 381 ms 195180 KB Output is correct
8 Correct 382 ms 195320 KB Output is correct
9 Correct 381 ms 194320 KB Output is correct
10 Correct 384 ms 196152 KB Output is correct
11 Correct 383 ms 196088 KB Output is correct
12 Correct 75 ms 53368 KB Output is correct
13 Correct 149 ms 93224 KB Output is correct
14 Correct 235 ms 134268 KB Output is correct
15 Correct 383 ms 196100 KB Output is correct
16 Correct 402 ms 196128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 760 KB Output is correct
6 Correct 2 ms 760 KB Output is correct
7 Correct 2 ms 760 KB Output is correct
8 Correct 2 ms 760 KB Output is correct
9 Correct 2 ms 760 KB Output is correct
10 Correct 2 ms 756 KB Output is correct
11 Correct 2 ms 760 KB Output is correct
12 Correct 2 ms 760 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 2 ms 632 KB Output is correct
15 Correct 2 ms 696 KB Output is correct
16 Correct 2 ms 760 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 7 ms 4820 KB Output is correct
19 Correct 4 ms 4444 KB Output is correct
20 Correct 7 ms 4856 KB Output is correct
21 Correct 7 ms 4688 KB Output is correct
22 Correct 6 ms 4728 KB Output is correct
23 Correct 6 ms 4344 KB Output is correct
24 Correct 5 ms 3320 KB Output is correct
25 Correct 6 ms 5112 KB Output is correct
26 Correct 6 ms 5112 KB Output is correct
27 Correct 6 ms 4984 KB Output is correct
28 Correct 6 ms 4728 KB Output is correct
29 Correct 6 ms 4732 KB Output is correct
30 Correct 5 ms 4216 KB Output is correct
31 Correct 6 ms 4088 KB Output is correct
32 Correct 6 ms 4344 KB Output is correct
33 Correct 8 ms 4856 KB Output is correct
34 Correct 6 ms 4728 KB Output is correct
35 Correct 7 ms 4600 KB Output is correct
36 Correct 7 ms 4472 KB Output is correct
37 Correct 5 ms 3576 KB Output is correct
38 Correct 5 ms 3960 KB Output is correct
39 Correct 6 ms 4472 KB Output is correct
40 Correct 2 ms 1012 KB Output is correct
41 Correct 5 ms 3192 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 395 ms 196204 KB Output is correct
44 Correct 415 ms 195192 KB Output is correct
45 Correct 387 ms 196212 KB Output is correct
46 Correct 383 ms 196184 KB Output is correct
47 Correct 396 ms 196064 KB Output is correct
48 Correct 381 ms 195180 KB Output is correct
49 Correct 382 ms 195320 KB Output is correct
50 Correct 381 ms 194320 KB Output is correct
51 Correct 384 ms 196152 KB Output is correct
52 Correct 383 ms 196088 KB Output is correct
53 Correct 75 ms 53368 KB Output is correct
54 Correct 149 ms 93224 KB Output is correct
55 Correct 235 ms 134268 KB Output is correct
56 Correct 383 ms 196100 KB Output is correct
57 Correct 402 ms 196128 KB Output is correct
58 Correct 309 ms 167556 KB Output is correct
59 Correct 317 ms 180948 KB Output is correct
60 Correct 300 ms 178496 KB Output is correct
61 Correct 292 ms 173900 KB Output is correct
62 Correct 373 ms 196188 KB Output is correct
63 Correct 386 ms 196108 KB Output is correct
64 Correct 385 ms 195232 KB Output is correct
65 Correct 357 ms 187144 KB Output is correct
66 Correct 294 ms 174384 KB Output is correct
67 Correct 281 ms 159068 KB Output is correct
68 Correct 299 ms 175736 KB Output is correct
69 Correct 294 ms 176408 KB Output is correct
70 Correct 304 ms 175736 KB Output is correct
71 Correct 296 ms 183800 KB Output is correct
72 Correct 169 ms 178448 KB Output is correct
73 Correct 18 ms 13304 KB Output is correct