답안 #731905

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
731905 2023-04-28T06:42:58 Z GrindMachine Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
229 ms 57140 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*



*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct fenwick {
    int siz;
    vector<T> tree;

    fenwick(int n) {
        siz = n;
        tree = vector<T>(n + 1);
    }

    int lsb(int x) {
        return x & -x;
    }

    void build(vector<T> &a, int n) {
        for (int i = 1; i <= n; ++i) {
            int par = i + lsb(i);
            tree[i] += a[i];

            if (par <= siz) {
                tree[par] += tree[i];
            }
        }
    }

    void pupd(int i, T v) {
        while (i <= siz) {
            tree[i] += v;
            i += lsb(i);
        }
    }

    T sum(int i) {
        T res = 0;

        while (i) {
            res += tree[i];
            i -= lsb(i);
        }

        return res;
    }

    T query(int l, int r) {
        if (l > r) return 0;
        T res = sum(r) - sum(l - 1);
        return res;
    }
};

void solve(int test_case)
{
    ll n; cin >> n;
    vector<ll> a(n + 5);
    vector<ll> pos[3];

    rep1(i, n) {
        char ch; cin >> ch;
        if (ch == 'R') a[i] = 0, pos[0].pb(i);
        else if (ch == 'G') a[i] = 1, pos[1].pb(i);
        else a[i] = 2, pos[2].pb(i);
    }

    ll s1 = sz(pos[0]);
    ll s2 = sz(pos[1]);
    ll s3 = sz(pos[2]);

    ll dp[s1 + 1][s2 + 1][s3 + 1][3];
    memset(dp, 0x3f, sizeof dp);

    rep(p, 3) {
        dp[0][0][0][p] = 0;
    }

    rep(i, s1 + 1) {
        rep(j, s2 + 1) {
            fenwick<ll> fenw(n + 5);
            rep(i1, i) {
                fenw.pupd(pos[0][i1], 1);
            }
            rep(j1, j) {
                fenw.pupd(pos[1][j1], 1);
            }

            rep(k, s3 + 1) {
                if (k - 1 >= 0) {
                    fenw.pupd(pos[2][k - 1], 1);
                }

                rep(p, 3) {
                    rep(x, 3) {
                        if (x == p) conts;

                        ll ind = -1;

                        if (x == 0) {
                            if (i != s1) {
                                ind = pos[0][i];
                            }
                        }

                        if (x == 1) {
                            if (j != s2) {
                                ind = pos[1][j];
                            }
                        }

                        if (x == 2) {
                            if (k != s3) {
                                ind = pos[2][k];
                            }
                        }

                        if (ind == -1) conts;

                        ll moves = ind - 1 - fenw.query(1, ind - 1);
                        amin(dp[i + (x == 0)][j + (x == 1)][k + (x == 2)][x], dp[i][j][k][p] + moves);
                    }
                }
            }
        }
    }

    ll ans = inf2;
    rep(p, 3) {
        amin(ans, dp[s1][s2][s3][p]);
    }

    if (ans >= inf2) ans = -1;
    cout << ans << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 324 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 324 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 448 KB Output is correct
28 Correct 1 ms 444 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 324 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 76 ms 1236 KB Output is correct
3 Correct 76 ms 1256 KB Output is correct
4 Correct 86 ms 1236 KB Output is correct
5 Correct 84 ms 1256 KB Output is correct
6 Correct 79 ms 1264 KB Output is correct
7 Correct 83 ms 1260 KB Output is correct
8 Correct 86 ms 1260 KB Output is correct
9 Correct 83 ms 1252 KB Output is correct
10 Correct 84 ms 1264 KB Output is correct
11 Correct 83 ms 1260 KB Output is correct
12 Correct 11 ms 468 KB Output is correct
13 Correct 30 ms 724 KB Output is correct
14 Correct 47 ms 852 KB Output is correct
15 Correct 85 ms 1260 KB Output is correct
16 Correct 83 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 324 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 448 KB Output is correct
28 Correct 1 ms 444 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 324 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 440 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 76 ms 1236 KB Output is correct
44 Correct 76 ms 1256 KB Output is correct
45 Correct 86 ms 1236 KB Output is correct
46 Correct 84 ms 1256 KB Output is correct
47 Correct 79 ms 1264 KB Output is correct
48 Correct 83 ms 1260 KB Output is correct
49 Correct 86 ms 1260 KB Output is correct
50 Correct 83 ms 1252 KB Output is correct
51 Correct 84 ms 1264 KB Output is correct
52 Correct 83 ms 1260 KB Output is correct
53 Correct 11 ms 468 KB Output is correct
54 Correct 30 ms 724 KB Output is correct
55 Correct 47 ms 852 KB Output is correct
56 Correct 85 ms 1260 KB Output is correct
57 Correct 83 ms 1236 KB Output is correct
58 Correct 206 ms 56652 KB Output is correct
59 Correct 228 ms 56632 KB Output is correct
60 Correct 229 ms 56196 KB Output is correct
61 Correct 215 ms 56780 KB Output is correct
62 Correct 89 ms 4948 KB Output is correct
63 Correct 100 ms 8404 KB Output is correct
64 Correct 155 ms 28136 KB Output is correct
65 Correct 185 ms 40408 KB Output is correct
66 Correct 202 ms 55916 KB Output is correct
67 Correct 209 ms 55928 KB Output is correct
68 Correct 211 ms 56780 KB Output is correct
69 Correct 211 ms 57140 KB Output is correct
70 Correct 213 ms 56900 KB Output is correct
71 Correct 209 ms 55756 KB Output is correct
72 Correct 66 ms 14528 KB Output is correct
73 Correct 12 ms 3988 KB Output is correct