Submission #985808

# Submission time Handle Problem Language Result Execution time Memory
985808 2024-05-18T23:39:26 Z vyshniak_n Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
60 / 100
81 ms 185756 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][210];

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 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 2 ms 8660 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6500 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 2 ms 8660 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6500 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 11 ms 25176 KB Output is correct
19 Correct 3 ms 20828 KB Output is correct
20 Correct 5 ms 26972 KB Output is correct
21 Correct 5 ms 23132 KB Output is correct
22 Correct 7 ms 27088 KB Output is correct
23 Correct 3 ms 19032 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 5 ms 29276 KB Output is correct
28 Correct 3 ms 23132 KB Output is correct
29 Correct 3 ms 24924 KB Output is correct
30 Correct 3 ms 19036 KB Output is correct
31 Correct 3 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 1 ms 468 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 14684 KB Output is correct
38 Correct 2 ms 16988 KB Output is correct
39 Correct 3 ms 21084 KB Output is correct
40 Correct 1 ms 344 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 Incorrect 81 ms 185756 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 2 ms 8660 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6500 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 11 ms 25176 KB Output is correct
19 Correct 3 ms 20828 KB Output is correct
20 Correct 5 ms 26972 KB Output is correct
21 Correct 5 ms 23132 KB Output is correct
22 Correct 7 ms 27088 KB Output is correct
23 Correct 3 ms 19032 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 5 ms 29276 KB Output is correct
28 Correct 3 ms 23132 KB Output is correct
29 Correct 3 ms 24924 KB Output is correct
30 Correct 3 ms 19036 KB Output is correct
31 Correct 3 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 1 ms 468 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 14684 KB Output is correct
38 Correct 2 ms 16988 KB Output is correct
39 Correct 3 ms 21084 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 2396 KB Output is correct
43 Incorrect 81 ms 185756 KB Output isn't correct
44 Halted 0 ms 0 KB -