Submission #167298

#TimeUsernameProblemLanguageResultExecution timeMemory
167298combi1k1Growing Vegetable is Fun 3 (JOI19_ho_t3)C++14
100 / 100
161 ms95884 KiB
#include<bits/stdc++.h>

using namespace std;

#define sz(x)   (int)x.size()

const int   N   = 201;

int f[N][N][N][3];

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;      cin >> n;
    string s;   cin >> s;

    vector<int> pos[3];
    vector<int> ord[3];

    for(int i = 0 ; i < n ; ++i)    {
        if (s[i] == 'R')    pos[0].push_back(i + 1);
        if (s[i] == 'G')    pos[1].push_back(i + 1);
        if (s[i] == 'Y')    pos[2].push_back(i + 1);
    }

    if (sz(pos[0]) > (n + 1) / 2)   return  0 * puts("-1");
    if (sz(pos[1]) > (n + 1) / 2)   return  0 * puts("-1");
    if (sz(pos[2]) > (n + 1) / 2)   return  0 * puts("-1");

    memset(f,0x3f3f3f3f,sizeof f);

    if (sz(pos[0]))     f[1][0][0][0] = pos[0][0] - 1;
    if (sz(pos[1]))     f[0][1][0][1] = pos[1][0] - 1;
    if (sz(pos[2]))     f[0][0][1][2] = pos[2][0] - 1;

    for(int i = 0 ; i < 3 ; ++i)    {

        for(int j = 0 ; j <= n ; ++j)
        ord[i].push_back(upper_bound(pos[i].begin(),pos[i].end(),j) - pos[i].begin());
        pos[i].insert(pos[i].begin(),0);
    }

    for(int a = 0 ; a < sz(pos[0]) ; ++a)
    for(int b = 0 ; b < sz(pos[1]) ; ++b)
    for(int c = 0 ; c < sz(pos[2]) ; ++c)    {
        int P = a + b + c;
        if (P < 2)  continue;

        for(int x = 0 ; x < 3 ; ++x)    {
            int na = a - (x == 0);
            int nb = b - (x == 1);
            int nc = c - (x == 2);

            if (na < 0) continue;
            if (nb < 0) continue;
            if (nc < 0) continue;

            int real_pos;

            if (x == 0) real_pos = pos[0][a] + max(0,b - ord[1][pos[0][a]]) + max(0,c - ord[2][pos[0][a]]);
            if (x == 1) real_pos = pos[1][b] + max(0,a - ord[0][pos[1][b]]) + max(0,c - ord[2][pos[1][b]]);
            if (x == 2) real_pos = pos[2][c] + max(0,a - ord[0][pos[2][c]]) + max(0,b - ord[1][pos[2][c]]);

            f[a][b][c][x] = min(f[na][nb][nc][0 + (x == 0)],f[na][nb][nc][2 - (x == 2)]) + abs(real_pos - P);
        }
    }

    int ans = 0x3f3f3f3f;

    ans = min(ans,f[pos[0].size() - 1][pos[1].size() - 1][pos[2].size() - 1][0]);
    ans = min(ans,f[pos[0].size() - 1][pos[1].size() - 1][pos[2].size() - 1][1]);
    ans = min(ans,f[pos[0].size() - 1][pos[1].size() - 1][pos[2].size() - 1][2]);

    cout << ans << endl;
}

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:65:105: warning: 'real_pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
             f[a][b][c][x] = min(f[na][nb][nc][0 + (x == 0)],f[na][nb][nc][2 - (x == 2)]) + abs(real_pos - P);
                                                                                                ~~~~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...