Submission #762256

# Submission time Handle Problem Language Result Execution time Memory
762256 2023-06-21T07:28:57 Z sysia Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
130 ms 84236 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
 
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 
// #define int long long
typedef pair<int, int> T;
const int oo = 1e9+7, oo2 = 1e9+7, K = 30;
 
int ti(char c){
    if (c == 'R') return 0;
    if (c == 'G') return 1;
    return 2;
}
 
void ckmin(int &a, int b){
    a = min(a, b);
}
 
void solve(){
    int n; cin >> n;
    string s; cin >> s;
    vector<int>cnt(3);
    for (auto c: s) cnt[ti(c)]++;
    int mx = 0;
    for (auto x: cnt) mx = max(mx, x);
    int p = 0;
    int a = -1, b = -1;
    for (int i = 0; i<3; i++) {
        if (cnt[i] == mx){
            p = i;
        }
    }
    for (int i = 0; i<3; i++){
        if (i == p) continue;
        if (a == -1) a = i;
        else b = i;
    }
    debug(p, a, b);
    s = "$" + s;
    vector<vector<int>>pos(3, {-oo});
    for (int i = 1; i<=n; i++){
        pos[ti(s[i])].emplace_back(i);
    }
    int dp[n+1][cnt[a]+1][cnt[b]+1][3];
    for (int i = 0; i<=n; i++){
        for (int A = 0; A <= cnt[a]; A++){
            for (int B = 0; B <= cnt[b]; B++){
                for (int last = 0; last < 3; last++){
                    dp[i][A][B][last] = oo;
                }
            }
        }
    }
    auto ile = [&](int l, int r, int where){
        int x = (int)(upper_bound(pos[where].begin(), pos[where].end(), r) - upper_bound(pos[where].begin(), pos[where].end(), l));
        return max(0, x);
    };
    //opt -- szukanie ile() nie zalezy od wartosci i, tylko od A, B, C
    //preprocess [now, A], [now, B], [now, C] dla kazdego now \in [1, n] oraz sensownego A, B, C
    vector pre(n+1, vector<vector<int>>(3));
    for (int now = 1; now <= n; now++){
        for (int rep = 0; rep < 3; rep++){
            pre[now][rep].resize(cnt[rep]+1);
            for (int i = 1; i<=cnt[rep]; i++){
                pre[now][rep][i] = ile(now, pos[rep][i], rep);
            }
        }
    }

    for (int i = 0; i<3; i++) dp[0][0][0][i] = 0;
    for (int i = 1; i<=n; i++){
        for (int A = 0; A <= cnt[a]; A++){
            for (int B = 0; B <= min(cnt[b], i-A); B++){
                int C = i - A - B;
                if (C < 0 || C > cnt[p]) continue;
                for (int last = 0; last < 3; last++){
                    for (int prev = 0; prev < 3; prev++){
                        if (last == prev) continue;
                        if (last == a){
                            if (!A) continue;
                            int now = pos[last][A];
                            int cost = now-i + pre[now][b][B] + pre[now][p][C];
                            ckmin(dp[i][A][B][last], dp[i-1][A-1][B][prev] + cost);
                        } else if (last == b){
                            if (!B) continue;
                            int now = pos[last][B];
                            int cost = now-i + pre[now][a][A] + pre[now][p][C];
                            ckmin(dp[i][A][B][last], dp[i-1][A][B-1][prev] + cost);
                        } else {
                            if (!C) continue;
                            int now = pos[p][C];
                            int cost = now-i + pre[now][b][B] + pre[now][a][A];
                            ckmin(dp[i][A][B][last], dp[i-1][A][B][prev] + cost);
                        }
                    }
                }
            }
        }
    }
    int ans = oo;
    for (int last = 0; last < 3; last++){
        ans = min(ans, dp[n][cnt[a]][cnt[b]][last]);
    }
    if (ans == oo) cout << "-1\n";
    else cout << ans << "\n";
}
 
int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    solve();
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 572 KB Output is correct
21 Correct 1 ms 572 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 596 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 468 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 1 ms 340 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 580 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 596 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 8 ms 1940 KB Output is correct
3 Correct 7 ms 1876 KB Output is correct
4 Correct 8 ms 1852 KB Output is correct
5 Correct 8 ms 1856 KB Output is correct
6 Correct 8 ms 1856 KB Output is correct
7 Correct 8 ms 1856 KB Output is correct
8 Correct 8 ms 1876 KB Output is correct
9 Correct 7 ms 1876 KB Output is correct
10 Correct 8 ms 1876 KB Output is correct
11 Correct 8 ms 1876 KB Output is correct
12 Correct 2 ms 708 KB Output is correct
13 Correct 4 ms 980 KB Output is correct
14 Correct 7 ms 1340 KB Output is correct
15 Correct 7 ms 1876 KB Output is correct
16 Correct 8 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 572 KB Output is correct
21 Correct 1 ms 572 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 596 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 468 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 1 ms 340 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 580 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 596 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 320 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 8 ms 1940 KB Output is correct
44 Correct 7 ms 1876 KB Output is correct
45 Correct 8 ms 1852 KB Output is correct
46 Correct 8 ms 1856 KB Output is correct
47 Correct 8 ms 1856 KB Output is correct
48 Correct 8 ms 1856 KB Output is correct
49 Correct 8 ms 1876 KB Output is correct
50 Correct 7 ms 1876 KB Output is correct
51 Correct 8 ms 1876 KB Output is correct
52 Correct 8 ms 1876 KB Output is correct
53 Correct 2 ms 708 KB Output is correct
54 Correct 4 ms 980 KB Output is correct
55 Correct 7 ms 1340 KB Output is correct
56 Correct 7 ms 1876 KB Output is correct
57 Correct 8 ms 1876 KB Output is correct
58 Correct 112 ms 75724 KB Output is correct
59 Correct 123 ms 78764 KB Output is correct
60 Correct 111 ms 78868 KB Output is correct
61 Correct 128 ms 84044 KB Output is correct
62 Correct 16 ms 5332 KB Output is correct
63 Correct 18 ms 9312 KB Output is correct
64 Correct 47 ms 30624 KB Output is correct
65 Correct 85 ms 44492 KB Output is correct
66 Correct 101 ms 81740 KB Output is correct
67 Correct 95 ms 73804 KB Output is correct
68 Correct 103 ms 83408 KB Output is correct
69 Correct 123 ms 84236 KB Output is correct
70 Correct 130 ms 80972 KB Output is correct
71 Correct 123 ms 73804 KB Output is correct
72 Correct 26 ms 10452 KB Output is correct
73 Correct 11 ms 3412 KB Output is correct