답안 #1043005

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1043005 2024-08-03T17:42:26 Z SoulKnight Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
399 ms 129620 KB
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define ln '\n'


const int INF = 1e9;

struct Seg{
    int n;
    vector<int> seg;

    void init(int n_){
        n = n_;
        seg.assign(2*n, 0);
    }

    void upd(int p, int delta){
        for (seg[p += n] += delta; p > 1; p /= 2) seg[p/2] = seg[p] + seg[p^1];
    }

    int sum(int l, int r){
        int res = 0;
        for (l += n, r += n+1; l < r; l /= 2, r /= 2){
            if (l & 1) res += seg[l++];
            if (r & 1) res += seg[--r];
        }
        return res;
    }
} seg;



int n, sz[3];
string s;
int dp[405][405][405][4];
bool is_set[405][405][405][4];
vector<int> where[3];

int f(array<int, 4>& x){
    // for (int i = 0; i < 4; i++) cout << x[i] << " ";
    // cout << ln;
    if (is_set[x[0]][x[1]][x[2]][x[3]]) return dp[x[0]][x[1]][x[2]][x[3]];

    int ans = INF;
    int old_x3 = x[3];
    for (int i = 0; i < 3; i++){
        if (i == x[3]) continue; // prev moved
        if (x[i] == sz[i]) continue; // already at end

        // use where[i][x[i]]
        seg.upd(where[i][x[i]], 1);
        int t = seg.sum(where[i][x[i]]+1, n-1) + where[i][x[i]] - (x[0] + x[1] + x[2]);

        x[i]++; x[3] = i;

        ans = min(ans, t + (!is_set[x[0]][x[1]][x[2]][x[3]]? f(x): dp[x[0]][x[1]][x[2]][x[3]]));

        x[i]--; x[3] = old_x3;
        seg.upd(where[i][x[i]], -1);
    }

    dp[x[0]][x[1]][x[2]][x[3]] = min(ans, INF); // no overflow
    is_set[x[0]][x[1]][x[2]][x[3]] = 1;

    return dp[x[0]][x[1]][x[2]][x[3]];
}

void solve(){
    cin >> n >> s;
    seg.init(n);

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

    array<int, 4> xx = {0, 0, 0, 3};
    int ans = f(xx);
    cout << (ans == INF? -1: ans) << ln;

}


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

    // int TT; cin >> TT;
    // while (TT--) {solve();}

    solve();

}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 2 ms 3160 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 3164 KB Output is correct
22 Correct 1 ms 2256 KB Output is correct
23 Correct 2 ms 2976 KB Output is correct
24 Correct 1 ms 2140 KB Output is correct
25 Correct 0 ms 1116 KB Output is correct
26 Correct 1 ms 1372 KB Output is correct
27 Correct 2 ms 3164 KB Output is correct
28 Correct 3 ms 3416 KB Output is correct
29 Correct 2 ms 2908 KB Output is correct
30 Correct 2 ms 2908 KB Output is correct
31 Correct 2 ms 2776 KB Output is correct
32 Correct 2 ms 3164 KB Output is correct
33 Correct 1 ms 1628 KB Output is correct
34 Correct 1 ms 1884 KB Output is correct
35 Correct 2 ms 3164 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2216 KB Output is correct
38 Correct 2 ms 2656 KB Output is correct
39 Correct 2 ms 3168 KB Output is correct
40 Correct 0 ms 352 KB Output is correct
41 Correct 0 ms 1120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 352 KB Output is correct
2 Correct 2 ms 5216 KB Output is correct
3 Correct 2 ms 5216 KB Output is correct
4 Correct 2 ms 5332 KB Output is correct
5 Correct 4 ms 5220 KB Output is correct
6 Correct 2 ms 5228 KB Output is correct
7 Correct 2 ms 5220 KB Output is correct
8 Correct 3 ms 5220 KB Output is correct
9 Correct 2 ms 5224 KB Output is correct
10 Correct 2 ms 5224 KB Output is correct
11 Correct 2 ms 5224 KB Output is correct
12 Correct 2 ms 2920 KB Output is correct
13 Correct 2 ms 3688 KB Output is correct
14 Correct 3 ms 4456 KB Output is correct
15 Correct 3 ms 5224 KB Output is correct
16 Correct 3 ms 5344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 2 ms 3160 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 3164 KB Output is correct
22 Correct 1 ms 2256 KB Output is correct
23 Correct 2 ms 2976 KB Output is correct
24 Correct 1 ms 2140 KB Output is correct
25 Correct 0 ms 1116 KB Output is correct
26 Correct 1 ms 1372 KB Output is correct
27 Correct 2 ms 3164 KB Output is correct
28 Correct 3 ms 3416 KB Output is correct
29 Correct 2 ms 2908 KB Output is correct
30 Correct 2 ms 2908 KB Output is correct
31 Correct 2 ms 2776 KB Output is correct
32 Correct 2 ms 3164 KB Output is correct
33 Correct 1 ms 1628 KB Output is correct
34 Correct 1 ms 1884 KB Output is correct
35 Correct 2 ms 3164 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2216 KB Output is correct
38 Correct 2 ms 2656 KB Output is correct
39 Correct 2 ms 3168 KB Output is correct
40 Correct 0 ms 352 KB Output is correct
41 Correct 0 ms 1120 KB Output is correct
42 Correct 1 ms 352 KB Output is correct
43 Correct 2 ms 5216 KB Output is correct
44 Correct 2 ms 5216 KB Output is correct
45 Correct 2 ms 5332 KB Output is correct
46 Correct 4 ms 5220 KB Output is correct
47 Correct 2 ms 5228 KB Output is correct
48 Correct 2 ms 5220 KB Output is correct
49 Correct 3 ms 5220 KB Output is correct
50 Correct 2 ms 5224 KB Output is correct
51 Correct 2 ms 5224 KB Output is correct
52 Correct 2 ms 5224 KB Output is correct
53 Correct 2 ms 2920 KB Output is correct
54 Correct 2 ms 3688 KB Output is correct
55 Correct 3 ms 4456 KB Output is correct
56 Correct 3 ms 5224 KB Output is correct
57 Correct 3 ms 5344 KB Output is correct
58 Correct 380 ms 108384 KB Output is correct
59 Correct 393 ms 129620 KB Output is correct
60 Correct 385 ms 121940 KB Output is correct
61 Correct 397 ms 119380 KB Output is correct
62 Correct 6 ms 13144 KB Output is correct
63 Correct 14 ms 22620 KB Output is correct
64 Correct 83 ms 71016 KB Output is correct
65 Correct 167 ms 100696 KB Output is correct
66 Correct 393 ms 120404 KB Output is correct
67 Correct 388 ms 114844 KB Output is correct
68 Correct 383 ms 123676 KB Output is correct
69 Correct 399 ms 120148 KB Output is correct
70 Correct 397 ms 127156 KB Output is correct
71 Correct 386 ms 116288 KB Output is correct
72 Correct 19 ms 15452 KB Output is correct
73 Correct 2 ms 3416 KB Output is correct