Submission #1107382

# Submission time Handle Problem Language Result Execution time Memory
1107382 2024-11-01T07:27:14 Z vjudge1 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
281 ms 586824 KB
# include <bits/stdc++.h>

// #pragma optimize("g", on)
// #pragma GCC optimize ("inline")
// #pragma GCC optimize ("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC optimize ("03")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native")

// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")


using namespace std;
 
# define ll long long
# define pii pair <int, int>
# define pll pair <ll, ll>
# define nl '\n'
# define sz(x) (int)(x).size()
# define all(x) (x).begin(), (x).end()
# define deb(x) cerr << #x << " = " << x << endl;
 
const int maxn = (int)404;
const ll inf = (ll)2e5;
const int mod = (int)1e9 + 7;
const bool T = 0;

short n;
string s;
int dp[maxn][maxn][maxn][3];
short pref[3][maxn];
vector <short> g[3];
short d[3];

short f (char c) {
    if (c == 'R') return 0;
    if (c == 'G') return 1;
    if (c == 'Y') return 2;
    assert(0);
    return -1;
}

void op (int &a, int b) {
    a = min(a, b);
}

void ma1n () {
    cin >> n >> s;
    g[0].push_back(0);
    g[1].push_back(0);
    g[2].push_back(0);
    for (short i = 0; i < n; ++i) {
        for (short j = 0; j < 3; ++j) {
            pref[j][i + 1] = pref[j][i];
        }
        short x = f(s[i]);
        pref[x][i + 1]++;
        g[x].push_back(i + 1);
    }
    for (short i = 0; i <= n; ++i) {
        for (short x = 0; x <= pref[0][n]; ++x) {
            for (short y = 0; y <= pref[1][n]; ++y) {
                for (short k = 0; k < 3; ++k) {
                    dp[i][x][y][k] = inf;
                }
            }
        }
    }
    // memset(dp, 0x3f, sizeof dp);
    // int lim = dp[3][2][1][0];
    dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
    // deb(s);
    // exit(0);
    for (short i = 0; i < n; ++i) {
        for (short x = 0; x <= min(i, pref[0][n]); ++x) {
            for (short y = 0; y <= min(i, pref[1][n]); ++y) {
                if (x + y > i) continue;
                short z = i - x - y;
                if (z > pref[2][n]) continue;
                d[0] = x, d[1] = y, d[2] = z;
                for (short prv = 0; prv < 3; ++prv) {
                    for (short cur = 0; cur < 3; ++cur) {
                        if (prv == cur || d[cur] == pref[cur][n]) continue;
                        // cout << "continue everything" << nl;
                        // continue;
                        short othr = prv ^ cur ^ 3;
                        assert(othr >= 0 && othr < 3 && othr != prv && othr != cur);
                        short id = g[cur][d[cur] + 1];
                        op(dp[i + 1][x + (cur == 0)][y + (cur == 1)][cur], dp[i][x][y][prv] + max(pref[prv][id] - d[prv], 0) + max(pref[othr][id] - d[othr], 0));
                    }
                }

            }
        }
    }
    int ans = inf;
    for (short last = 0; last < 3; ++last) {
        ans = min(ans, dp[n][pref[0][n]][pref[1][n]][last]);
        // cout << pref[0][n] << ' ' << pref[1][n] << ' ' << last << ' ' <<dp[n][pref[0][n]][pref[1][n]][last] << nl;
    }
    // for (int x = 0; x <= n; ++x) {
    //     for (int y = 0; y <= n; ++y) {
    //         if (x + y > n) continue;
    //         for (int last = 0; last < 3; ++last) {
    //             ans = min(ans, dp[n][x][y][last]);
    //             if (dp[n][x][y][last] != lim) {
    //                 cout << x << ' ' << y << ' ' << last << ' ' << dp[n][x][y][last] << nl;
    //             }
    //         }
    //     }
    // }
    cout << (ans == inf ? -1 : ans) << nl;
}

signed main () {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int ttt = 1;
    if (T) cin >> ttt;
    while (ttt--) {
        ma1n();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 6480 KB Output is correct
4 Correct 3 ms 20816 KB Output is correct
5 Correct 4 ms 29008 KB Output is correct
6 Correct 4 ms 29008 KB Output is correct
7 Correct 4 ms 29008 KB Output is correct
8 Correct 4 ms 29008 KB Output is correct
9 Correct 4 ms 29008 KB Output is correct
10 Correct 3 ms 29008 KB Output is correct
11 Correct 3 ms 29008 KB Output is correct
12 Correct 4 ms 29008 KB Output is correct
13 Correct 4 ms 29020 KB Output is correct
14 Correct 4 ms 29008 KB Output is correct
15 Correct 4 ms 29200 KB Output is correct
16 Correct 4 ms 29228 KB Output is correct
17 Correct 4 ms 26960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 6480 KB Output is correct
4 Correct 3 ms 20816 KB Output is correct
5 Correct 4 ms 29008 KB Output is correct
6 Correct 4 ms 29008 KB Output is correct
7 Correct 4 ms 29008 KB Output is correct
8 Correct 4 ms 29008 KB Output is correct
9 Correct 4 ms 29008 KB Output is correct
10 Correct 3 ms 29008 KB Output is correct
11 Correct 3 ms 29008 KB Output is correct
12 Correct 4 ms 29008 KB Output is correct
13 Correct 4 ms 29020 KB Output is correct
14 Correct 4 ms 29008 KB Output is correct
15 Correct 4 ms 29200 KB Output is correct
16 Correct 4 ms 29228 KB Output is correct
17 Correct 4 ms 26960 KB Output is correct
18 Correct 12 ms 117328 KB Output is correct
19 Correct 12 ms 115280 KB Output is correct
20 Correct 13 ms 117480 KB Output is correct
21 Correct 11 ms 115280 KB Output is correct
22 Correct 12 ms 117528 KB Output is correct
23 Correct 11 ms 115448 KB Output is correct
24 Correct 12 ms 115348 KB Output is correct
25 Correct 13 ms 117392 KB Output is correct
26 Correct 13 ms 117328 KB Output is correct
27 Correct 11 ms 117328 KB Output is correct
28 Correct 11 ms 117328 KB Output is correct
29 Correct 12 ms 117348 KB Output is correct
30 Correct 13 ms 115280 KB Output is correct
31 Correct 12 ms 115304 KB Output is correct
32 Correct 12 ms 115396 KB Output is correct
33 Correct 12 ms 115364 KB Output is correct
34 Correct 11 ms 115280 KB Output is correct
35 Correct 11 ms 113232 KB Output is correct
36 Correct 12 ms 115300 KB Output is correct
37 Correct 10 ms 111196 KB Output is correct
38 Correct 12 ms 115280 KB Output is correct
39 Correct 12 ms 115280 KB Output is correct
40 Correct 13 ms 115280 KB Output is correct
41 Correct 11 ms 115368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6480 KB Output is correct
2 Correct 225 ms 474608 KB Output is correct
3 Correct 231 ms 474184 KB Output is correct
4 Correct 230 ms 482588 KB Output is correct
5 Correct 231 ms 478280 KB Output is correct
6 Correct 239 ms 475208 KB Output is correct
7 Correct 226 ms 470856 KB Output is correct
8 Correct 245 ms 465736 KB Output is correct
9 Correct 236 ms 463712 KB Output is correct
10 Correct 240 ms 465992 KB Output is correct
11 Correct 228 ms 469320 KB Output is correct
12 Correct 56 ms 236632 KB Output is correct
13 Correct 94 ms 299652 KB Output is correct
14 Correct 135 ms 370248 KB Output is correct
15 Correct 217 ms 473672 KB Output is correct
16 Correct 235 ms 474700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 6480 KB Output is correct
4 Correct 3 ms 20816 KB Output is correct
5 Correct 4 ms 29008 KB Output is correct
6 Correct 4 ms 29008 KB Output is correct
7 Correct 4 ms 29008 KB Output is correct
8 Correct 4 ms 29008 KB Output is correct
9 Correct 4 ms 29008 KB Output is correct
10 Correct 3 ms 29008 KB Output is correct
11 Correct 3 ms 29008 KB Output is correct
12 Correct 4 ms 29008 KB Output is correct
13 Correct 4 ms 29020 KB Output is correct
14 Correct 4 ms 29008 KB Output is correct
15 Correct 4 ms 29200 KB Output is correct
16 Correct 4 ms 29228 KB Output is correct
17 Correct 4 ms 26960 KB Output is correct
18 Correct 12 ms 117328 KB Output is correct
19 Correct 12 ms 115280 KB Output is correct
20 Correct 13 ms 117480 KB Output is correct
21 Correct 11 ms 115280 KB Output is correct
22 Correct 12 ms 117528 KB Output is correct
23 Correct 11 ms 115448 KB Output is correct
24 Correct 12 ms 115348 KB Output is correct
25 Correct 13 ms 117392 KB Output is correct
26 Correct 13 ms 117328 KB Output is correct
27 Correct 11 ms 117328 KB Output is correct
28 Correct 11 ms 117328 KB Output is correct
29 Correct 12 ms 117348 KB Output is correct
30 Correct 13 ms 115280 KB Output is correct
31 Correct 12 ms 115304 KB Output is correct
32 Correct 12 ms 115396 KB Output is correct
33 Correct 12 ms 115364 KB Output is correct
34 Correct 11 ms 115280 KB Output is correct
35 Correct 11 ms 113232 KB Output is correct
36 Correct 12 ms 115300 KB Output is correct
37 Correct 10 ms 111196 KB Output is correct
38 Correct 12 ms 115280 KB Output is correct
39 Correct 12 ms 115280 KB Output is correct
40 Correct 13 ms 115280 KB Output is correct
41 Correct 11 ms 115368 KB Output is correct
42 Correct 1 ms 6480 KB Output is correct
43 Correct 225 ms 474608 KB Output is correct
44 Correct 231 ms 474184 KB Output is correct
45 Correct 230 ms 482588 KB Output is correct
46 Correct 231 ms 478280 KB Output is correct
47 Correct 239 ms 475208 KB Output is correct
48 Correct 226 ms 470856 KB Output is correct
49 Correct 245 ms 465736 KB Output is correct
50 Correct 236 ms 463712 KB Output is correct
51 Correct 240 ms 465992 KB Output is correct
52 Correct 228 ms 469320 KB Output is correct
53 Correct 56 ms 236632 KB Output is correct
54 Correct 94 ms 299652 KB Output is correct
55 Correct 135 ms 370248 KB Output is correct
56 Correct 217 ms 473672 KB Output is correct
57 Correct 235 ms 474700 KB Output is correct
58 Correct 237 ms 364944 KB Output is correct
59 Correct 225 ms 400624 KB Output is correct
60 Correct 243 ms 400712 KB Output is correct
61 Correct 225 ms 391240 KB Output is correct
62 Correct 219 ms 502404 KB Output is correct
63 Correct 231 ms 475588 KB Output is correct
64 Correct 230 ms 465500 KB Output is correct
65 Correct 249 ms 428756 KB Output is correct
66 Correct 215 ms 394180 KB Output is correct
67 Correct 197 ms 364040 KB Output is correct
68 Correct 221 ms 395720 KB Output is correct
69 Correct 243 ms 397464 KB Output is correct
70 Correct 245 ms 386632 KB Output is correct
71 Correct 270 ms 401996 KB Output is correct
72 Correct 281 ms 586824 KB Output is correct
73 Correct 28 ms 205648 KB Output is correct