Submission #127684

# Submission time Handle Problem Language Result Execution time Memory
127684 2019-07-09T23:53:54 Z tri Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
494 ms 5244 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

const int INF = 1e8;

namespace debug {
    const int DEBUG = true;

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x);

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x);

    template<class T>
    void pr(const vector<T> &x);

    template<class T>
    void pr(const set<T> &x);

    template<class T1, class T2>
    void pr(const map<T1, T2> &x);

    template<class T>
    void pr(const T &x) { if (DEBUG) cout << x; }

    template<class T, class... Ts>
    void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }

    template<class T>
    void prIn(const T &x) {
        pr("{");
        bool fst = 1;
        for (auto &a : x) {
            pr(fst ? "" : ", ", a), fst = 0;
        }
        pr("}");
    }

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x) { prIn(x); }

    template<class T>
    void pr(const vector<T> &x) { prIn(x); }

    template<class T>
    void pr(const set<T> &x) { prIn(x); }

    template<class T1, class T2>
    void pr(const map<T1, T2> &x) { prIn(x); }

    void ps() { pr("\n"); }

    template<class Arg, class... Args>
    void ps(const Arg &first, const Args &... rest) {
        pr(first, " ");
        ps(rest...);
    }
}
using namespace debug;

int pre[3][500][500]; //index, ending color, red delta, green delta, (inferred) yellow delta
int nxt[3][500][500];
int color[500];
int cnt[500][3];
int loc[500][3];

int main() {
    int N;
    cin >> N;

    string colorStr;
    cin >> colorStr;

    cnt[0][0] = cnt[0][1] = cnt[0][2] = 0;

    for (int i = 1; i <= N; i++) {
        if (colorStr[i - 1] == 'R') {
            color[i] = 0;
        } else if (colorStr[i - 1] == 'G') {
            color[i] = 1;
        } else {
            color[i] = 2;
        }
        cnt[i][0] = cnt[i - 1][0];
        cnt[i][1] = cnt[i - 1][1];
        cnt[i][2] = cnt[i - 1][2];

        cnt[i][color[i]]++;

        loc[cnt[i][color[i]]][color[i]] = i;
    }

//    ps("read", N);
//
//    ps("allocate");

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < N; j++) {
            for (int k = 0; k < N; k++) {
                pre[i][j][k] = nxt[i][j][k] = INF;
            }
        }
    }

    pre[0][0][0] = 0;
    pre[1][0][0] = 0;
    pre[2][0][0] = 0;


    for (int i = 1; i <= N; i++) {
        for (int r = 0; r < N; r++) {
            for (int g = 0; g < N; g++) {
                int y = i - r - g;
                if (y < 0) {
                    continue;
                }

                // iterate over cases of last color
                if (r > 0) {
                    int oLoc = loc[r][0];
                    int eC = abs(cnt[oLoc][1] - g) + abs(cnt[oLoc][2] - y);
                    nxt[0][r][g] = min(pre[1][r - 1][g], pre[2][r - 1][g]) + eC;
                }
                if (g > 0) {
                    int oLoc = loc[g][1];
                    int eC = abs(cnt[oLoc][0] - r) + abs(cnt[oLoc][2] - y);
                    nxt[1][r][g] = min(pre[0][r][g - 1], pre[2][r][g - 1]) + eC;
                }
                if (y > 0) {
                    int oLoc = loc[y][2];
                    int eC = abs(cnt[oLoc][0] - r) + abs(cnt[oLoc][1] - g);
                    nxt[2][r][g] = min(pre[0][r][g], pre[1][r][g]) + eC;
                }
            }
        }

        for (int j = 0; j < 3; j++) {
            for (int k = 0; k <= N; k++) {
                for (int l = 0; l <= N; l++) {
//                    if (i - k - l >= 0 && i <= 2) {
//                        ps("end:", j, "cnts:", k, l, i - k - l, "cost:", nxt[j][k][l]);
//                    }
                    pre[j][k][l] = nxt[j][k][l];
                    nxt[j][k][l] = INF;
                }
            }
        }
    }

    int ans = min(pre[0][cnt[N][0]][cnt[N][1]], min(pre[1][cnt[N][0]][cnt[N][1]], pre[2][cnt[N][0]][cnt[N][1]]));
    if (ans >= INF) {
        cout << -1 << endl;
        return 0;
    }
    ans /= 2;
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 508 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 508 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 4 ms 1016 KB Output is correct
19 Correct 4 ms 1144 KB Output is correct
20 Correct 4 ms 1144 KB Output is correct
21 Correct 4 ms 1016 KB Output is correct
22 Correct 4 ms 1144 KB Output is correct
23 Correct 4 ms 1016 KB Output is correct
24 Correct 4 ms 1144 KB Output is correct
25 Correct 4 ms 1144 KB Output is correct
26 Correct 4 ms 1144 KB Output is correct
27 Correct 4 ms 1016 KB Output is correct
28 Correct 4 ms 1144 KB Output is correct
29 Correct 4 ms 1148 KB Output is correct
30 Correct 4 ms 1144 KB Output is correct
31 Correct 4 ms 1144 KB Output is correct
32 Correct 4 ms 1016 KB Output is correct
33 Correct 4 ms 1016 KB Output is correct
34 Correct 4 ms 1016 KB Output is correct
35 Correct 4 ms 1016 KB Output is correct
36 Correct 4 ms 1016 KB Output is correct
37 Correct 4 ms 1016 KB Output is correct
38 Correct 4 ms 1144 KB Output is correct
39 Correct 4 ms 1144 KB Output is correct
40 Correct 4 ms 1144 KB Output is correct
41 Correct 4 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 448 ms 5240 KB Output is correct
3 Correct 440 ms 5112 KB Output is correct
4 Correct 460 ms 5240 KB Output is correct
5 Correct 445 ms 5212 KB Output is correct
6 Correct 451 ms 5244 KB Output is correct
7 Correct 440 ms 5112 KB Output is correct
8 Correct 442 ms 5112 KB Output is correct
9 Correct 438 ms 5112 KB Output is correct
10 Correct 447 ms 5112 KB Output is correct
11 Correct 445 ms 5112 KB Output is correct
12 Correct 68 ms 2936 KB Output is correct
13 Correct 152 ms 3704 KB Output is correct
14 Correct 255 ms 4216 KB Output is correct
15 Correct 450 ms 5108 KB Output is correct
16 Correct 458 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 508 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 4 ms 1016 KB Output is correct
19 Correct 4 ms 1144 KB Output is correct
20 Correct 4 ms 1144 KB Output is correct
21 Correct 4 ms 1016 KB Output is correct
22 Correct 4 ms 1144 KB Output is correct
23 Correct 4 ms 1016 KB Output is correct
24 Correct 4 ms 1144 KB Output is correct
25 Correct 4 ms 1144 KB Output is correct
26 Correct 4 ms 1144 KB Output is correct
27 Correct 4 ms 1016 KB Output is correct
28 Correct 4 ms 1144 KB Output is correct
29 Correct 4 ms 1148 KB Output is correct
30 Correct 4 ms 1144 KB Output is correct
31 Correct 4 ms 1144 KB Output is correct
32 Correct 4 ms 1016 KB Output is correct
33 Correct 4 ms 1016 KB Output is correct
34 Correct 4 ms 1016 KB Output is correct
35 Correct 4 ms 1016 KB Output is correct
36 Correct 4 ms 1016 KB Output is correct
37 Correct 4 ms 1016 KB Output is correct
38 Correct 4 ms 1144 KB Output is correct
39 Correct 4 ms 1144 KB Output is correct
40 Correct 4 ms 1144 KB Output is correct
41 Correct 4 ms 1016 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 448 ms 5240 KB Output is correct
44 Correct 440 ms 5112 KB Output is correct
45 Correct 460 ms 5240 KB Output is correct
46 Correct 445 ms 5212 KB Output is correct
47 Correct 451 ms 5244 KB Output is correct
48 Correct 440 ms 5112 KB Output is correct
49 Correct 442 ms 5112 KB Output is correct
50 Correct 438 ms 5112 KB Output is correct
51 Correct 447 ms 5112 KB Output is correct
52 Correct 445 ms 5112 KB Output is correct
53 Correct 68 ms 2936 KB Output is correct
54 Correct 152 ms 3704 KB Output is correct
55 Correct 255 ms 4216 KB Output is correct
56 Correct 450 ms 5108 KB Output is correct
57 Correct 458 ms 5112 KB Output is correct
58 Correct 454 ms 5108 KB Output is correct
59 Correct 455 ms 5108 KB Output is correct
60 Correct 446 ms 5080 KB Output is correct
61 Correct 468 ms 5212 KB Output is correct
62 Correct 447 ms 5112 KB Output is correct
63 Correct 444 ms 5140 KB Output is correct
64 Correct 454 ms 5112 KB Output is correct
65 Correct 452 ms 5132 KB Output is correct
66 Correct 439 ms 4984 KB Output is correct
67 Correct 494 ms 5112 KB Output is correct
68 Correct 443 ms 5092 KB Output is correct
69 Correct 451 ms 5112 KB Output is correct
70 Correct 473 ms 5112 KB Output is correct
71 Correct 441 ms 4984 KB Output is correct
72 Correct 442 ms 5112 KB Output is correct
73 Correct 448 ms 5104 KB Output is correct