Submission #754628

# Submission time Handle Problem Language Result Execution time Memory
754628 2023-06-08T07:18:42 Z Kihihihi Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
75 / 100
500 ms 780508 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];

int after(vector<int> idx, vector<vector<int>>& pos, vector<vector<int>>& cnt, int nd)
{
    int res = 0;
    for (int i = 0; i < 3; i++)
    {
        if (i == nd)
        {
            continue;
        }

        if (idx[i] > 0 && pos[nd][idx[nd] - 1] < pos[i][idx[i] - 1])
        {
            res += cnt[i][pos[i][idx[i] - 1]] - cnt[i][pos[nd][idx[nd] - 1]];
        }
    }
    return res;
}

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;
                }

                vector<int> idx = { i, j, k };
                for (int lst = 0; lst < 3; lst++)
                {
                    if (idx[lst] == 0)
                    {
                        continue;
                    }

                    int aft = after(idx, pos, cnt, lst);
                    for (int pr = 0; pr < 3; pr++)
                    {
                        if (lst == pr)
                        {
                            continue;
                        }

                        int ii = i, jj = j, kk = k;
                        if (lst == 0)
                        {
                            ii--;
                        }
                        else if (lst == 1)
                        {
                            jj--;
                        }
                        else
                        {
                            kk--;
                        }

                        if (dp[ii][jj][kk][pr] != INF)
                        {
                            int npos = pos[lst][idx[lst] - 1] + aft;
                            int dist = abs(npos - (i + j + k - 1));
                            dp[i][j][k][lst] = min(dp[i][j][k][lst], dp[ii][jj][kk][pr] + 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:97:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |     for (int i = 0; i < pos[0].size() + 1; i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:99:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |         for (int j = 0; j < pos[1].size() + 1; j++)
      |                         ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:101:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |             for (int k = 0; k < pos[2].size() + 1; k++)
      |                             ~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 361 ms 780240 KB Output is correct
2 Correct 356 ms 780292 KB Output is correct
3 Correct 369 ms 780292 KB Output is correct
4 Correct 411 ms 780196 KB Output is correct
5 Correct 354 ms 780344 KB Output is correct
6 Correct 340 ms 780184 KB Output is correct
7 Correct 352 ms 780208 KB Output is correct
8 Correct 346 ms 780176 KB Output is correct
9 Correct 352 ms 780208 KB Output is correct
10 Correct 365 ms 780364 KB Output is correct
11 Correct 340 ms 780196 KB Output is correct
12 Correct 334 ms 780232 KB Output is correct
13 Correct 333 ms 780176 KB Output is correct
14 Correct 376 ms 780236 KB Output is correct
15 Correct 340 ms 780300 KB Output is correct
16 Correct 344 ms 780304 KB Output is correct
17 Correct 350 ms 780240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 361 ms 780240 KB Output is correct
2 Correct 356 ms 780292 KB Output is correct
3 Correct 369 ms 780292 KB Output is correct
4 Correct 411 ms 780196 KB Output is correct
5 Correct 354 ms 780344 KB Output is correct
6 Correct 340 ms 780184 KB Output is correct
7 Correct 352 ms 780208 KB Output is correct
8 Correct 346 ms 780176 KB Output is correct
9 Correct 352 ms 780208 KB Output is correct
10 Correct 365 ms 780364 KB Output is correct
11 Correct 340 ms 780196 KB Output is correct
12 Correct 334 ms 780232 KB Output is correct
13 Correct 333 ms 780176 KB Output is correct
14 Correct 376 ms 780236 KB Output is correct
15 Correct 340 ms 780300 KB Output is correct
16 Correct 344 ms 780304 KB Output is correct
17 Correct 350 ms 780240 KB Output is correct
18 Correct 355 ms 780252 KB Output is correct
19 Correct 342 ms 780280 KB Output is correct
20 Correct 338 ms 780224 KB Output is correct
21 Correct 338 ms 780220 KB Output is correct
22 Correct 329 ms 780300 KB Output is correct
23 Correct 360 ms 780244 KB Output is correct
24 Correct 335 ms 780264 KB Output is correct
25 Correct 354 ms 780212 KB Output is correct
26 Correct 353 ms 780240 KB Output is correct
27 Correct 326 ms 780296 KB Output is correct
28 Correct 365 ms 780272 KB Output is correct
29 Correct 352 ms 780212 KB Output is correct
30 Correct 339 ms 780220 KB Output is correct
31 Correct 327 ms 780236 KB Output is correct
32 Correct 350 ms 780508 KB Output is correct
33 Correct 325 ms 780260 KB Output is correct
34 Correct 344 ms 780240 KB Output is correct
35 Correct 349 ms 780300 KB Output is correct
36 Correct 328 ms 780248 KB Output is correct
37 Correct 326 ms 780228 KB Output is correct
38 Correct 322 ms 780280 KB Output is correct
39 Correct 343 ms 780292 KB Output is correct
40 Correct 356 ms 780244 KB Output is correct
41 Correct 359 ms 780344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 337 ms 780180 KB Output is correct
2 Correct 333 ms 780308 KB Output is correct
3 Correct 347 ms 780272 KB Output is correct
4 Correct 337 ms 780240 KB Output is correct
5 Correct 335 ms 780248 KB Output is correct
6 Correct 346 ms 780284 KB Output is correct
7 Correct 352 ms 780196 KB Output is correct
8 Correct 341 ms 780436 KB Output is correct
9 Correct 347 ms 780296 KB Output is correct
10 Correct 341 ms 780252 KB Output is correct
11 Correct 345 ms 780188 KB Output is correct
12 Correct 327 ms 780264 KB Output is correct
13 Correct 346 ms 780260 KB Output is correct
14 Correct 352 ms 780312 KB Output is correct
15 Correct 336 ms 780420 KB Output is correct
16 Correct 333 ms 780336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 361 ms 780240 KB Output is correct
2 Correct 356 ms 780292 KB Output is correct
3 Correct 369 ms 780292 KB Output is correct
4 Correct 411 ms 780196 KB Output is correct
5 Correct 354 ms 780344 KB Output is correct
6 Correct 340 ms 780184 KB Output is correct
7 Correct 352 ms 780208 KB Output is correct
8 Correct 346 ms 780176 KB Output is correct
9 Correct 352 ms 780208 KB Output is correct
10 Correct 365 ms 780364 KB Output is correct
11 Correct 340 ms 780196 KB Output is correct
12 Correct 334 ms 780232 KB Output is correct
13 Correct 333 ms 780176 KB Output is correct
14 Correct 376 ms 780236 KB Output is correct
15 Correct 340 ms 780300 KB Output is correct
16 Correct 344 ms 780304 KB Output is correct
17 Correct 350 ms 780240 KB Output is correct
18 Correct 355 ms 780252 KB Output is correct
19 Correct 342 ms 780280 KB Output is correct
20 Correct 338 ms 780224 KB Output is correct
21 Correct 338 ms 780220 KB Output is correct
22 Correct 329 ms 780300 KB Output is correct
23 Correct 360 ms 780244 KB Output is correct
24 Correct 335 ms 780264 KB Output is correct
25 Correct 354 ms 780212 KB Output is correct
26 Correct 353 ms 780240 KB Output is correct
27 Correct 326 ms 780296 KB Output is correct
28 Correct 365 ms 780272 KB Output is correct
29 Correct 352 ms 780212 KB Output is correct
30 Correct 339 ms 780220 KB Output is correct
31 Correct 327 ms 780236 KB Output is correct
32 Correct 350 ms 780508 KB Output is correct
33 Correct 325 ms 780260 KB Output is correct
34 Correct 344 ms 780240 KB Output is correct
35 Correct 349 ms 780300 KB Output is correct
36 Correct 328 ms 780248 KB Output is correct
37 Correct 326 ms 780228 KB Output is correct
38 Correct 322 ms 780280 KB Output is correct
39 Correct 343 ms 780292 KB Output is correct
40 Correct 356 ms 780244 KB Output is correct
41 Correct 359 ms 780344 KB Output is correct
42 Correct 337 ms 780180 KB Output is correct
43 Correct 333 ms 780308 KB Output is correct
44 Correct 347 ms 780272 KB Output is correct
45 Correct 337 ms 780240 KB Output is correct
46 Correct 335 ms 780248 KB Output is correct
47 Correct 346 ms 780284 KB Output is correct
48 Correct 352 ms 780196 KB Output is correct
49 Correct 341 ms 780436 KB Output is correct
50 Correct 347 ms 780296 KB Output is correct
51 Correct 341 ms 780252 KB Output is correct
52 Correct 345 ms 780188 KB Output is correct
53 Correct 327 ms 780264 KB Output is correct
54 Correct 346 ms 780260 KB Output is correct
55 Correct 352 ms 780312 KB Output is correct
56 Correct 336 ms 780420 KB Output is correct
57 Correct 333 ms 780336 KB Output is correct
58 Execution timed out 516 ms 780340 KB Time limit exceeded
59 Halted 0 ms 0 KB -