Submission #499378

# Submission time Handle Problem Language Result Execution time Memory
499378 2021-12-28T08:45:00 Z armashka Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
72 ms 61024 KB
#include <bits/stdc++.h>
 
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
#define fast ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define all(v) v.begin(),v.end()
#define pb push_back
#define sz size()
#define ft first
#define sd second
 
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef unsigned long long ull;
 
const int N = 2e6 + 5;
const ll M = 1e8;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
const double Pi = acos(-1); 
 
ll binpow(ll x, ll ti) { ll res = 1;while (ti){if(ti & 1)res *= x;x *= x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll binmul(ll x, ll ti) { ll res = 0;while (ti){if(ti & 1)res += x;x += x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll nok(ll a, ll b) { return (a*b)/__gcd(abs(a),abs(b)) * (a*b > 0 ? 1 : -1); }
bool odd(ll n) { return (n % 2 == 1); }
bool even(ll n) { return (n % 2 == 0); }            
 
ll n, r, g, y, pref[5][405], pos[5][405];
string s;
vector<vector<vector<vector<ll>>>> dp;
 
const void solve(/*Armashka*/) {
	cin >> n >> s;
	s = " " + s;
	for (int i = 1; i <= n; ++ i) {
		for (int j = 1; j <= 3; ++ j) pref[j][i] = pref[j][i - 1];
		if (s[i] == 'R') ++ pref[1][i], pos[1][++ r] = i;
		if (s[i] == 'G') ++ pref[2][i], pos[2][++ g] = i;
		if (s[i] == 'Y') ++ pref[3][i], pos[3][++ y] = i;
	}		
	dp.resize(4);
	for (int c = 1; c <= 3; ++ c) {
		dp[c].resize(r + 5);
		for (int i = 0; i <= r; ++ i) {
			dp[c][i].resize(g + 5);
			for (int j = 0; j <= g; ++ j) {
				dp[c][i][j].resize(y + 5);
				for (int k = 0; k <= y; ++ k) dp[c][i][j][k] = inf;
			}
		}
	}
	
	dp[1][0][0][0] = dp[2][0][0][0] = dp[3][0][0][0] = 0;
	for (int i = 0; i <= r; ++ i) {
		for (int j = 0; j <= g; ++ j) {
			for (int k = 0; k <= y; ++ k) {
				if (i) {
					ll x = max(0ll, pref[2][pos[1][i]] - j) + max(0ll, pref[3][pos[1][i]] - k);
					dp[1][i][j][k] = min({dp[1][i][j][k], dp[2][i - 1][j][k] + x, dp[3][i - 1][j][k] + x});
				}
				if (j) {
					ll x = max(0ll, pref[1][pos[2][j]] - i) + max(0ll, pref[3][pos[2][j]] - k);
					dp[2][i][j][k] = min({dp[2][i][j][k], dp[1][i][j - 1][k] + x, dp[3][i][j - 1][k] + x});
				}
				if (k) {
					ll x = max(0ll, pref[1][pos[3][k]] - i) + max(0ll, pref[2][pos[3][k]] - j);
					dp[3][i][j][k] = min({dp[3][i][j][k], dp[1][i][j][k - 1] + x, dp[2][i][j][k - 1] + x});
				}
			}
		}
	}
	ll ans = min({dp[1][r][g][y], dp[2][r][g][y], dp[3][r][g][y]});
	if (ans == inf) ans = -1;
	cout << ans << "\n";
}
            
signed main() {
    fast;
    //freopen("triangles.in", "r", stdin);
    //freopen("triangles.out", "w", stdout);
    int tt = 1;
	//cin >> tt;
    while (tt --) {                                                             
        solve();
    }
}
 
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 1 ms 588 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 588 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 588 KB Output is correct
28 Correct 1 ms 588 KB Output is correct
29 Correct 1 ms 588 KB Output is correct
30 Correct 1 ms 588 KB Output is correct
31 Correct 1 ms 588 KB Output is correct
32 Correct 1 ms 588 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 1 ms 460 KB Output is correct
35 Correct 1 ms 588 KB Output is correct
36 Correct 1 ms 588 KB Output is correct
37 Correct 1 ms 460 KB Output is correct
38 Correct 1 ms 588 KB Output is correct
39 Correct 1 ms 588 KB Output is correct
40 Correct 0 ms 332 KB Output is correct
41 Correct 0 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 14 ms 8948 KB Output is correct
3 Correct 13 ms 8796 KB Output is correct
4 Correct 14 ms 8844 KB Output is correct
5 Correct 15 ms 8884 KB Output is correct
6 Correct 10 ms 8948 KB Output is correct
7 Correct 9 ms 8900 KB Output is correct
8 Correct 10 ms 8908 KB Output is correct
9 Correct 9 ms 8776 KB Output is correct
10 Correct 10 ms 8940 KB Output is correct
11 Correct 10 ms 8888 KB Output is correct
12 Correct 3 ms 2636 KB Output is correct
13 Correct 5 ms 4300 KB Output is correct
14 Correct 7 ms 6220 KB Output is correct
15 Correct 10 ms 8908 KB Output is correct
16 Correct 10 ms 8836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 1 ms 588 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 588 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 588 KB Output is correct
28 Correct 1 ms 588 KB Output is correct
29 Correct 1 ms 588 KB Output is correct
30 Correct 1 ms 588 KB Output is correct
31 Correct 1 ms 588 KB Output is correct
32 Correct 1 ms 588 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 1 ms 460 KB Output is correct
35 Correct 1 ms 588 KB Output is correct
36 Correct 1 ms 588 KB Output is correct
37 Correct 1 ms 460 KB Output is correct
38 Correct 1 ms 588 KB Output is correct
39 Correct 1 ms 588 KB Output is correct
40 Correct 0 ms 332 KB Output is correct
41 Correct 0 ms 460 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 14 ms 8948 KB Output is correct
44 Correct 13 ms 8796 KB Output is correct
45 Correct 14 ms 8844 KB Output is correct
46 Correct 15 ms 8884 KB Output is correct
47 Correct 10 ms 8948 KB Output is correct
48 Correct 9 ms 8900 KB Output is correct
49 Correct 10 ms 8908 KB Output is correct
50 Correct 9 ms 8776 KB Output is correct
51 Correct 10 ms 8940 KB Output is correct
52 Correct 10 ms 8888 KB Output is correct
53 Correct 3 ms 2636 KB Output is correct
54 Correct 5 ms 4300 KB Output is correct
55 Correct 7 ms 6220 KB Output is correct
56 Correct 10 ms 8908 KB Output is correct
57 Correct 10 ms 8836 KB Output is correct
58 Correct 66 ms 59544 KB Output is correct
59 Correct 70 ms 60484 KB Output is correct
60 Correct 67 ms 60100 KB Output is correct
61 Correct 67 ms 60484 KB Output is correct
62 Correct 18 ms 12328 KB Output is correct
63 Correct 19 ms 15896 KB Output is correct
64 Correct 38 ms 34560 KB Output is correct
65 Correct 54 ms 46632 KB Output is correct
66 Correct 69 ms 59708 KB Output is correct
67 Correct 69 ms 59236 KB Output is correct
68 Correct 69 ms 60624 KB Output is correct
69 Correct 72 ms 61024 KB Output is correct
70 Correct 72 ms 60620 KB Output is correct
71 Correct 71 ms 58948 KB Output is correct
72 Correct 23 ms 16460 KB Output is correct
73 Correct 4 ms 4044 KB Output is correct