Submission #141011

# Submission time Handle Problem Language Result Execution time Memory
141011 2019-08-06T11:06:52 Z meatrow Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
15 / 100
446 ms 5516 KB
//#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

const int N = 401;

int dp[N][N][4][2];
int cnt[3];
int n;

void init(int q) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            for (int t = 0; t < 4; t++) {
                dp[i][j][t][q] = INT32_MAX;
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n;
    map<char, int> mp;
    mp['R'] = 0;
    mp['G'] = 1;
    mp['Y'] = 2;
    string s;
    cin >> s;
    vector<int> pos[3];
    for (int i = 0; i < n; i++) {
        cnt[mp[s[i]]]++;
        pos[mp[s[i]]].push_back(i);
    }
    for (int i = 0; i < 3; i++) {
        pos[i].push_back(-1);
        reverse(pos[i].begin(), pos[i].end());
    }
    init(0);
    dp[cnt[0]][cnt[1]][3][0] = 0;
    for (int i = 0; i < n; i++) {
        int q = i & 1;
        int w = q ^ 1;
        init(w);
        for (int j = 0; j <= n; j++) {
            for (int k = 0; k <= n; k++) {
                for (int t = 0; t < 4; t++) {
                    if (dp[j][k][t][q] == INT32_MAX) {
                        continue;
                    }
                    if (j > 0 && t != 0) {
                        dp[j - 1][k][0][w] = min(dp[j - 1][k][0][w], dp[j][k][t][q] + abs(pos[0][j] - i));
                    }
                    if (k > 0 && t != 1) {
                        dp[j][k - 1][1][w] = min(dp[j][k - 1][1][w], dp[j][k][t][q] + abs(pos[1][k] - i));
                    }
                    if (n - i - j - k > 0 && t != 2) {
                        dp[j][k][2][w] = min(dp[j][k][2][w], dp[j][k][t][q] + abs(pos[2][n - i - j - k] - i));
                    }
                }
            }
        }
    }
    int ans = INT32_MAX;
    for (int i = 0; i < 4; i++) {
        ans = min(ans, dp[0][0][i][n & 1]);
    }
    if (ans == INT32_MAX) {
        cout << -1;
    } else {
        if (ans & 1) {
            return 1;
        }
        cout << ans / 2;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5368 KB Output is correct
2 Correct 8 ms 5372 KB Output is correct
3 Correct 9 ms 5368 KB Output is correct
4 Correct 11 ms 5368 KB Output is correct
5 Correct 12 ms 5368 KB Output is correct
6 Correct 12 ms 5368 KB Output is correct
7 Correct 12 ms 5368 KB Output is correct
8 Correct 12 ms 5368 KB Output is correct
9 Correct 12 ms 5368 KB Output is correct
10 Correct 12 ms 5368 KB Output is correct
11 Incorrect 13 ms 5368 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5368 KB Output is correct
2 Correct 8 ms 5372 KB Output is correct
3 Correct 9 ms 5368 KB Output is correct
4 Correct 11 ms 5368 KB Output is correct
5 Correct 12 ms 5368 KB Output is correct
6 Correct 12 ms 5368 KB Output is correct
7 Correct 12 ms 5368 KB Output is correct
8 Correct 12 ms 5368 KB Output is correct
9 Correct 12 ms 5368 KB Output is correct
10 Correct 12 ms 5368 KB Output is correct
11 Incorrect 13 ms 5368 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5368 KB Output is correct
2 Correct 442 ms 5428 KB Output is correct
3 Correct 439 ms 5496 KB Output is correct
4 Correct 438 ms 5368 KB Output is correct
5 Correct 446 ms 5456 KB Output is correct
6 Correct 445 ms 5496 KB Output is correct
7 Correct 439 ms 5368 KB Output is correct
8 Correct 434 ms 5452 KB Output is correct
9 Correct 434 ms 5372 KB Output is correct
10 Correct 436 ms 5468 KB Output is correct
11 Correct 438 ms 5496 KB Output is correct
12 Correct 120 ms 5516 KB Output is correct
13 Correct 200 ms 5496 KB Output is correct
14 Correct 288 ms 5452 KB Output is correct
15 Correct 438 ms 5368 KB Output is correct
16 Correct 438 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5368 KB Output is correct
2 Correct 8 ms 5372 KB Output is correct
3 Correct 9 ms 5368 KB Output is correct
4 Correct 11 ms 5368 KB Output is correct
5 Correct 12 ms 5368 KB Output is correct
6 Correct 12 ms 5368 KB Output is correct
7 Correct 12 ms 5368 KB Output is correct
8 Correct 12 ms 5368 KB Output is correct
9 Correct 12 ms 5368 KB Output is correct
10 Correct 12 ms 5368 KB Output is correct
11 Incorrect 13 ms 5368 KB Output isn't correct
12 Halted 0 ms 0 KB -