제출 #391334

#제출 시각아이디문제언어결과실행 시간메모리
391334timmyfengGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
60 / 100
989 ms757412 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 401;

int count_r[N], count_g[N], count_y[N];
int where_r[N], where_g[N], where_y[N];
int swaps[N][N][N][3];

void set_min(int &a, int b) {
    a = min(a, b);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

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

    for (int i = 0; i < n; ++i) {
        count_r[i + 1] = count_r[i] + (s[i] == 'R');
        count_g[i + 1] = count_g[i] + (s[i] == 'G');
        count_y[i + 1] = count_y[i] + (s[i] == 'Y');

        if (s[i] == 'R') {
            where_r[count_r[i]] = i;
        } else if (s[i] == 'G') {
            where_g[count_g[i]] = i;
        } else {
            where_y[count_y[i]] = i;
        }
    }

    for (int r = 0; r <= n; ++r) {
        for (int g = 0; g <= n; ++g) {
            for (int y = 0; y <= n; ++y) {
                for (int i = 0; i < 3; ++i) {
                    if (r + g + y > 0) {
                        swaps[r][g][y][i] = INT_MAX;
                    }
                }
            }
        }
    }

    for (int r = 0; r <= n; ++r) {
        for (int g = 0; g <= n; ++g) {
            for (int y = 0; y <= n; ++y) {
                for (int i = 0; i < 3; ++i) {
                    if (swaps[r][g][y][i] < INT_MAX) {
                        if (i != 0 && r < count_r[n]) {
                            set_min(swaps[r + 1][g][y][0], swaps[r][g][y][i] +
                                max(0, count_g[where_r[r]] - g) +
                                max(0, count_y[where_r[r]] - y)
                            );
                        }

                        if (i != 1 && g < count_g[n]) {
                            set_min(swaps[r][g + 1][y][1], swaps[r][g][y][i] +
                                max(0, count_r[where_g[g]] - r) +
                                max(0, count_y[where_g[g]] - y)
                            );
                        }

                        if (i != 2 && y < count_y[n]) {
                            set_min(swaps[r][g][y + 1][2], swaps[r][g][y][i] +
                                max(0, count_r[where_y[y]] - r) +
                                max(0, count_g[where_y[y]] - g)
                            );
                        }
                    }
                }
            }
        }
    }

    int ans = INT_MAX;
    for (int i = 0; i < 3; ++i) {
        set_min(ans, swaps[count_r[n]][count_g[n]][count_y[n]][i]);
    }

    cout << (ans < INT_MAX ? ans : -1) << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...