Submission #985810

# Submission time Handle Problem Language Result Execution time Memory
985810 2024-05-18T23:45:46 Z vyshniak_n Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
53 ms 188244 KB
//#pragma optimize("Ofast")
//#pragma optimize("unroll-loops")
//#pragma traget("avx,avx2")

#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <random>
#include <deque>
#include <functional>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <complex>
#include <numeric>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <thread>

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define el '\n'
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define point pair <ll, ll>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;

#include <random>
mt19937 rnd(time(0));

const ll INF = 1e18 + 10;
const ll inf = 1e9 + 10;
const ll N = 2e5 + 10;
const ll mod = 1e9 + 7;
const ll K = 200;
const ll LOG = 8;

ll dp[210][210][210][3], pref[3][410];

void solve() {
    ll n;
    cin >> n;

    string s;
    cin >> s;
    s = '#' + s;

    map <char, ll> mp;
    mp['R'] = 0, mp['G'] = 1, mp['Y'] = 2;

    vector <ll> a(n + 1);
    for (ll i = 1; i <= n; i++)
        a[i] = mp[s[i]];

    ll cnt[3] = {0, 0, 0};
    for (ll i = 1; i <= n; i++)
        cnt[a[i]]++;

    if (max({cnt[0], cnt[1], cnt[2]}) > (n + 1) / 2)
        return void(cout << -1 << el);

    for (ll i = 0; i < 3; i++)
        for (ll j = 1; j <= n; j++)
            pref[i][j] = pref[i][j - 1] + (a[j] == i);

    vector <ll> pos[3];
    pos[0].pb(0), pos[1].pb(0), pos[2].pb(0);
    for (ll i = 1; i <= n; i++)
        pos[a[i]].pb(i);

    for (ll i = 0; i <= cnt[0]; i++)
        for (ll j = 0; j <= cnt[1]; j++)
            for (ll k = 0; k <= cnt[2]; k++)
                for (ll cur = 0; cur < 3; cur++)
                    dp[i][j][k][cur] = inf;

    dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;

    for (ll i = 0; i <= cnt[0]; i++)
        for (ll j = 0; j <= cnt[1]; j++)
            for (ll k = 0; k <= cnt[2]; k++)
                for (ll cur = 0; cur < 3; cur++) {
                    if (i + j + k == 0)
                        continue;

                    if (cur == 0) {
                        if (i == 0)
                            continue;

                        ll val = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]);

                        ll now = pos[cur][i];
                        now += max(0ll, j - pref[1][pos[cur][i]]);
                        now += max(0ll, k - pref[2][pos[cur][i]]);
                        val += now - i - j - k;

                        dp[i][j][k][cur] = min(dp[i][j][k][cur], val);
                    }
                    if (cur == 1) {
                        if (j == 0)
                            continue;

                        ll val = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]);

                        ll now = pos[cur][j];
                        now += max(0ll, i - pref[0][pos[cur][j]]);
                        now += max(0ll, k - pref[2][pos[cur][j]]);
                        val += now - i - j - k;

                        dp[i][j][k][cur] = min(dp[i][j][k][cur], val);
                    }
                    if (cur == 2) {
                        if (k == 0)
                            continue;

                        ll val = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]);

                        ll now = pos[cur][k];
                        now += max(0ll, i - pref[0][pos[cur][k]]);
                        now += max(0ll, j - pref[1][pos[cur][k]]);
                        val += now - i - j - k;

                        dp[i][j][k][cur] = min(dp[i][j][k][cur], val);
                    }
                }

    ll ans = min({dp[cnt[0]][cnt[1]][cnt[2]][0],
                  dp[cnt[0]][cnt[1]][cnt[2]][1],
                  dp[cnt[0]][cnt[1]][cnt[2]][2]});

    cout << ans << el;
    return;
}
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int tests = 1;
    //cin >> tests;

    while (tests--) 
        solve();
    return 0;
}
/*
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 432 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 432 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 4 ms 25180 KB Output is correct
19 Correct 3 ms 20824 KB Output is correct
20 Correct 3 ms 27228 KB Output is correct
21 Correct 3 ms 23132 KB Output is correct
22 Correct 3 ms 26972 KB Output is correct
23 Correct 3 ms 19032 KB Output is correct
24 Correct 2 ms 12892 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 3 ms 29276 KB Output is correct
28 Correct 3 ms 23132 KB Output is correct
29 Correct 3 ms 25180 KB Output is correct
30 Correct 3 ms 19036 KB Output is correct
31 Correct 2 ms 16988 KB Output is correct
32 Correct 3 ms 21084 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 3 ms 25180 KB Output is correct
36 Correct 3 ms 22876 KB Output is correct
37 Correct 2 ms 14940 KB Output is correct
38 Correct 2 ms 16988 KB Output is correct
39 Correct 3 ms 21084 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 39 ms 186420 KB Output is correct
3 Correct 45 ms 185852 KB Output is correct
4 Correct 38 ms 188132 KB Output is correct
5 Correct 40 ms 188004 KB Output is correct
6 Correct 38 ms 188136 KB Output is correct
7 Correct 37 ms 187216 KB Output is correct
8 Correct 39 ms 187452 KB Output is correct
9 Correct 39 ms 186876 KB Output is correct
10 Correct 38 ms 187984 KB Output is correct
11 Correct 40 ms 188244 KB Output is correct
12 Correct 12 ms 109148 KB Output is correct
13 Correct 18 ms 131364 KB Output is correct
14 Correct 26 ms 152156 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 432 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 4 ms 25180 KB Output is correct
19 Correct 3 ms 20824 KB Output is correct
20 Correct 3 ms 27228 KB Output is correct
21 Correct 3 ms 23132 KB Output is correct
22 Correct 3 ms 26972 KB Output is correct
23 Correct 3 ms 19032 KB Output is correct
24 Correct 2 ms 12892 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 3 ms 29276 KB Output is correct
28 Correct 3 ms 23132 KB Output is correct
29 Correct 3 ms 25180 KB Output is correct
30 Correct 3 ms 19036 KB Output is correct
31 Correct 2 ms 16988 KB Output is correct
32 Correct 3 ms 21084 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 3 ms 25180 KB Output is correct
36 Correct 3 ms 22876 KB Output is correct
37 Correct 2 ms 14940 KB Output is correct
38 Correct 2 ms 16988 KB Output is correct
39 Correct 3 ms 21084 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 2396 KB Output is correct
43 Correct 39 ms 186420 KB Output is correct
44 Correct 45 ms 185852 KB Output is correct
45 Correct 38 ms 188132 KB Output is correct
46 Correct 40 ms 188004 KB Output is correct
47 Correct 38 ms 188136 KB Output is correct
48 Correct 37 ms 187216 KB Output is correct
49 Correct 39 ms 187452 KB Output is correct
50 Correct 39 ms 186876 KB Output is correct
51 Correct 38 ms 187984 KB Output is correct
52 Correct 40 ms 188244 KB Output is correct
53 Correct 12 ms 109148 KB Output is correct
54 Correct 18 ms 131364 KB Output is correct
55 Correct 26 ms 152156 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 39 ms 123484 KB Output is correct
59 Correct 46 ms 139516 KB Output is correct
60 Correct 44 ms 135504 KB Output is correct
61 Correct 53 ms 130384 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 43 ms 186636 KB Output is correct
64 Correct 51 ms 182108 KB Output is correct
65 Correct 51 ms 159932 KB Output is correct
66 Correct 46 ms 132436 KB Output is correct
67 Correct 37 ms 116824 KB Output is correct
68 Correct 43 ms 132696 KB Output is correct
69 Correct 42 ms 132956 KB Output is correct
70 Correct 45 ms 132956 KB Output is correct
71 Correct 44 ms 139612 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 1 ms 344 KB Output is correct