Submission #754666

# Submission time Handle Problem Language Result Execution time Memory
754666 2023-06-08T09:31:45 Z Kihihihi Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
397 ms 780420 KB
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cassert>
#include <ctime>
#include <chrono>
#include <cstdio>
#include <random>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <fstream>
#include <functional>
#include <complex>
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 1e9, MOD = 1e9 + 7, MOD2 = 998244353, LOG = 18;
const long double EPS = 1e-9, PI = acos(-1);

const int N = 405;

int dp[N][N][N][3];

void solve()
{
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            for (int k = 0; k < N; k++)
            {
                for (int lst = 0; lst < 3; lst++)
                {
                    dp[i][j][k][lst] = INF;
                }
            }
        }
    }
    for (int lst = 0; lst < 3; lst++)
    {
        dp[0][0][0][lst] = 0;
    }

    int n;
    string s;
    cin >> n >> s;
    vector<vector<int>> pos(3), cnt(3, vector<int>(n));
    for (int i = 0; i < n; i++)
    {
        if (s[i] == 'R')
        {
            pos[0].push_back(i);
        }
        else if (s[i] == 'G')
        {
            pos[1].push_back(i);
        }
        else
        {
            pos[2].push_back(i);
        }

        for (int j = 0; j < 3; j++)
        {
            cnt[j][i] = pos[j].size();
        }
    }

    for (int i = 0; i < pos[0].size() + 1; i++)
    {
        for (int j = 0; j < pos[1].size() + 1; j++)
        {
            for (int k = 0; k < pos[2].size() + 1; k++)
            {
                if (max({ i, j, k }) >= (i + j + k + 1) / 2 + 1)
                {
                    continue;
                }

                if (i > 0)
                {
                    int aft = max(j - cnt[1][pos[0][i - 1]], 0) + max(k - cnt[2][pos[0][i - 1]], 0);
                    int npos = pos[0][i - 1] + aft;
                    int dist = abs(npos - (i + j + k - 1));
                    dp[i][j][k][0] = min(dp[i][j][k][0], min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + dist);
                }
                if (j > 0)
                {
                    int aft = max(i - cnt[0][pos[1][j - 1]], 0) + max(k - cnt[2][pos[1][j - 1]], 0);
                    int npos = pos[1][j - 1] + aft;
                    int dist = abs(npos - (i + j + k - 1));
                    dp[i][j][k][1] = min(dp[i][j][k][1], min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + dist);
                }
                if (k > 0)
                {
                    int aft = max(i - cnt[0][pos[2][k - 1]], 0) + max(j - cnt[1][pos[2][k - 1]], 0);
                    int npos = pos[2][k - 1] + aft;
                    int dist = abs(npos - (i + j + k - 1));
                    dp[i][j][k][2] = min(dp[i][j][k][2], min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + dist);
                }
            }
        }
    }

    int ans = INF;
    for (int lst = 0; lst < 3; lst++)
    {
        ans = min(ans, dp[pos[0].size()][pos[1].size()][pos[2].size()][lst]);
    }
    cout << (ans == INF ? -1 : ans) << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    srand(time(NULL));

    int tst = 1;
    //cin >> tst;
    while (tst--)
    {
        solve();
    }

    return 0;
}

/*
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠤⠖⠚⢉⣩⣭⡭⠛⠓⠲⠦⣄⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢀⡴⠋⠁⠀⠀⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠳⢦⡀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠃⢀⡴⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀
⠀⠀⠀⠀⡾⠁⣠⠋⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀
⠀⠀⠀⣸⠁⢰⠃⠀⠀⠀⠈⢣⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣇⠀
⠀⠀⠀⡇⠀⡾⡀⠀⠀⠀⠀⣀⣹⣆⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⠀
⠀⠀⢸⠃⢀⣇⡈⠀⠀⠀⠀⠀⠀⢀⡑⢄⡀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⢸⠀⢻⡟⡻⢶⡆⠀⠀⠀⠀⡼⠟⡳⢿⣦⡑⢄⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⣸⠀⢸⠃⡇⢀⠇⠀⠀⠀⠀⠀⡼⠀⠀⠈⣿⡗⠂⠀⠀⠀⠀⠀⠀⠀⢸⠁
⠀⠀⡏⠀⣼⠀⢳⠊⠀⠀⠀⠀⠀⠀⠱⣀⣀⠔⣸⠁⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀
⠀⠀⡇⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⠃⠀
⠀⢸⠃⠘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠁⠀⠀⢀⠀⠀⠀⠀⠀⣾⠀⠀
⠀⣸⠀⠀⠹⡄⠀⠀⠈⠁⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠸⠀⠀⠀⠀⠀⡇⠀⠀
⠀⡏⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⢀⣠⢶⡇⠀⠀⢰⡀⠀⠀⠀⠀⠀⡇⠀⠀
⢰⠇⡄⠀⠀⠀⡿⢣⣀⣀⣀⡤⠴⡞⠉⠀⢸⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⣧⠀⠀
⣸⠀⡇⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⢹⠀⠀⢸⠀⠀⢀⣿⠇⠀⠀⠀⠁⠀⢸⠀⠀
⣿⠀⡇⠀⠀⠀⠀⠀⢀⡤⠤⠶⠶⠾⠤⠄⢸⠀⡀⠸⣿⣀⠀⠀⠀⠀⠀⠈⣇⠀
⡇⠀⡇⠀⠀⡀⠀⡴⠋⠀⠀⠀⠀⠀⠀⠀⠸⡌⣵⡀⢳⡇⠀⠀⠀⠀⠀⠀⢹⡀
⡇⠀⠇⠀⠀⡇⡸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠮⢧⣀⣻⢂⠀⠀⠀⠀⠀⠀⢧
⣇⠀⢠⠀⠀⢳⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡎⣆⠀⠀⠀⠀⠀⠘
⢻⠀⠈⠰⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⠘⢮⣧⡀⠀⠀⠀⠀
⠸⡆⠀⠀⠇⣾⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠆⠀⠀⠀⠀⠀⠀⠀⠙⠳⣄⡀⢢⡀
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
*/

Compilation message

joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for (int i = 0; i < pos[0].size() + 1; i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:81:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for (int j = 0; j < pos[1].size() + 1; j++)
      |                         ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:83:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |             for (int k = 0; k < pos[2].size() + 1; k++)
      |                             ~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 368 ms 780276 KB Output is correct
2 Correct 363 ms 780248 KB Output is correct
3 Correct 335 ms 780420 KB Output is correct
4 Correct 348 ms 780364 KB Output is correct
5 Correct 338 ms 780320 KB Output is correct
6 Correct 350 ms 780244 KB Output is correct
7 Correct 338 ms 780192 KB Output is correct
8 Correct 346 ms 780236 KB Output is correct
9 Correct 350 ms 780288 KB Output is correct
10 Correct 346 ms 780296 KB Output is correct
11 Correct 367 ms 780308 KB Output is correct
12 Correct 332 ms 780236 KB Output is correct
13 Correct 331 ms 780268 KB Output is correct
14 Correct 368 ms 780212 KB Output is correct
15 Correct 363 ms 780232 KB Output is correct
16 Correct 351 ms 780348 KB Output is correct
17 Correct 335 ms 780216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 368 ms 780276 KB Output is correct
2 Correct 363 ms 780248 KB Output is correct
3 Correct 335 ms 780420 KB Output is correct
4 Correct 348 ms 780364 KB Output is correct
5 Correct 338 ms 780320 KB Output is correct
6 Correct 350 ms 780244 KB Output is correct
7 Correct 338 ms 780192 KB Output is correct
8 Correct 346 ms 780236 KB Output is correct
9 Correct 350 ms 780288 KB Output is correct
10 Correct 346 ms 780296 KB Output is correct
11 Correct 367 ms 780308 KB Output is correct
12 Correct 332 ms 780236 KB Output is correct
13 Correct 331 ms 780268 KB Output is correct
14 Correct 368 ms 780212 KB Output is correct
15 Correct 363 ms 780232 KB Output is correct
16 Correct 351 ms 780348 KB Output is correct
17 Correct 335 ms 780216 KB Output is correct
18 Correct 347 ms 780236 KB Output is correct
19 Correct 331 ms 780400 KB Output is correct
20 Correct 336 ms 780284 KB Output is correct
21 Correct 339 ms 780372 KB Output is correct
22 Correct 340 ms 780244 KB Output is correct
23 Correct 350 ms 780308 KB Output is correct
24 Correct 350 ms 780236 KB Output is correct
25 Correct 340 ms 780328 KB Output is correct
26 Correct 346 ms 780236 KB Output is correct
27 Correct 342 ms 780208 KB Output is correct
28 Correct 341 ms 780248 KB Output is correct
29 Correct 334 ms 780292 KB Output is correct
30 Correct 331 ms 780304 KB Output is correct
31 Correct 329 ms 780240 KB Output is correct
32 Correct 348 ms 780320 KB Output is correct
33 Correct 334 ms 780224 KB Output is correct
34 Correct 338 ms 780276 KB Output is correct
35 Correct 335 ms 780244 KB Output is correct
36 Correct 336 ms 780244 KB Output is correct
37 Correct 339 ms 780272 KB Output is correct
38 Correct 331 ms 780208 KB Output is correct
39 Correct 359 ms 780272 KB Output is correct
40 Correct 334 ms 780248 KB Output is correct
41 Correct 338 ms 780384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 341 ms 780228 KB Output is correct
2 Correct 339 ms 780216 KB Output is correct
3 Correct 348 ms 780200 KB Output is correct
4 Correct 336 ms 780252 KB Output is correct
5 Correct 338 ms 780236 KB Output is correct
6 Correct 345 ms 780292 KB Output is correct
7 Correct 334 ms 780260 KB Output is correct
8 Correct 348 ms 780344 KB Output is correct
9 Correct 332 ms 780232 KB Output is correct
10 Correct 332 ms 780288 KB Output is correct
11 Correct 348 ms 780188 KB Output is correct
12 Correct 342 ms 780236 KB Output is correct
13 Correct 335 ms 780212 KB Output is correct
14 Correct 339 ms 780308 KB Output is correct
15 Correct 347 ms 780204 KB Output is correct
16 Correct 335 ms 780260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 368 ms 780276 KB Output is correct
2 Correct 363 ms 780248 KB Output is correct
3 Correct 335 ms 780420 KB Output is correct
4 Correct 348 ms 780364 KB Output is correct
5 Correct 338 ms 780320 KB Output is correct
6 Correct 350 ms 780244 KB Output is correct
7 Correct 338 ms 780192 KB Output is correct
8 Correct 346 ms 780236 KB Output is correct
9 Correct 350 ms 780288 KB Output is correct
10 Correct 346 ms 780296 KB Output is correct
11 Correct 367 ms 780308 KB Output is correct
12 Correct 332 ms 780236 KB Output is correct
13 Correct 331 ms 780268 KB Output is correct
14 Correct 368 ms 780212 KB Output is correct
15 Correct 363 ms 780232 KB Output is correct
16 Correct 351 ms 780348 KB Output is correct
17 Correct 335 ms 780216 KB Output is correct
18 Correct 347 ms 780236 KB Output is correct
19 Correct 331 ms 780400 KB Output is correct
20 Correct 336 ms 780284 KB Output is correct
21 Correct 339 ms 780372 KB Output is correct
22 Correct 340 ms 780244 KB Output is correct
23 Correct 350 ms 780308 KB Output is correct
24 Correct 350 ms 780236 KB Output is correct
25 Correct 340 ms 780328 KB Output is correct
26 Correct 346 ms 780236 KB Output is correct
27 Correct 342 ms 780208 KB Output is correct
28 Correct 341 ms 780248 KB Output is correct
29 Correct 334 ms 780292 KB Output is correct
30 Correct 331 ms 780304 KB Output is correct
31 Correct 329 ms 780240 KB Output is correct
32 Correct 348 ms 780320 KB Output is correct
33 Correct 334 ms 780224 KB Output is correct
34 Correct 338 ms 780276 KB Output is correct
35 Correct 335 ms 780244 KB Output is correct
36 Correct 336 ms 780244 KB Output is correct
37 Correct 339 ms 780272 KB Output is correct
38 Correct 331 ms 780208 KB Output is correct
39 Correct 359 ms 780272 KB Output is correct
40 Correct 334 ms 780248 KB Output is correct
41 Correct 338 ms 780384 KB Output is correct
42 Correct 341 ms 780228 KB Output is correct
43 Correct 339 ms 780216 KB Output is correct
44 Correct 348 ms 780200 KB Output is correct
45 Correct 336 ms 780252 KB Output is correct
46 Correct 338 ms 780236 KB Output is correct
47 Correct 345 ms 780292 KB Output is correct
48 Correct 334 ms 780260 KB Output is correct
49 Correct 348 ms 780344 KB Output is correct
50 Correct 332 ms 780232 KB Output is correct
51 Correct 332 ms 780288 KB Output is correct
52 Correct 348 ms 780188 KB Output is correct
53 Correct 342 ms 780236 KB Output is correct
54 Correct 335 ms 780212 KB Output is correct
55 Correct 339 ms 780308 KB Output is correct
56 Correct 347 ms 780204 KB Output is correct
57 Correct 335 ms 780260 KB Output is correct
58 Correct 352 ms 780392 KB Output is correct
59 Correct 397 ms 780220 KB Output is correct
60 Correct 349 ms 780280 KB Output is correct
61 Correct 352 ms 780200 KB Output is correct
62 Correct 341 ms 780292 KB Output is correct
63 Correct 338 ms 780236 KB Output is correct
64 Correct 336 ms 780412 KB Output is correct
65 Correct 344 ms 780312 KB Output is correct
66 Correct 365 ms 780396 KB Output is correct
67 Correct 346 ms 780256 KB Output is correct
68 Correct 365 ms 780304 KB Output is correct
69 Correct 356 ms 780264 KB Output is correct
70 Correct 367 ms 780320 KB Output is correct
71 Correct 369 ms 780300 KB Output is correct
72 Correct 341 ms 780284 KB Output is correct
73 Correct 336 ms 780280 KB Output is correct