Submission #234877

# Submission time Handle Problem Language Result Execution time Memory
234877 2020-05-26T06:44:13 Z wiwiho Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
182 ms 2844 KB
//#define NDEBUG

#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define pob pop_back()
#define pof pop_front()
#define F first
#define S second
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}
#define pii pair<int, int>
#define pll pair<ll, ll>
#define tiii tuple<int, int, int>
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) ((a) / (b) + !!((a) % (b)))

//#define TEST

typedef long long ll;
typedef unsigned long long ull;

using namespace std;
using namespace __gnu_pbds;

const ll MOD = 1000000007;
const ll MAX = 2147483647;

template<typename A, typename B>
ostream &operator<<(ostream &o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}


int main(){
    StarBurstStream

    int n;
    cin >> n;

    string s;
    cin >> s;

    vector<int> rp, gp, yp;
    vector<int> rc(n), gc(n), yc(n);
    for(int i = 0; i < n; i++){
        if(s[i] == 'R') rp.eb(i), rc[i]++;
        else if(s[i] == 'G') gp.eb(i), gc[i]++;
        else yp.eb(i), yc[i]++;
        if(i) rc[i] += rc[i - 1], gc[i] += gc[i - 1], yc[i] += yc[i - 1];
    }

    vector<vector<ll>> dpr(rp.size() + 1, vector<ll>(gp.size() + 1, MAX));
    vector<vector<ll>> dpg(rp.size() + 1, vector<ll>(gp.size() + 1, MAX));
    vector<vector<ll>> dpy(rp.size() + 1, vector<ll>(gp.size() + 1, MAX));
    dpr[0][0] = 0;
    dpg[0][0] = 0;
    dpy[0][0] = 0;

    for(int i = 1; i <= n; i++){
        vector<vector<ll>> dpr2(rp.size() + 1, vector<ll>(gp.size() + 1, MAX));
        vector<vector<ll>> dpg2(rp.size() + 1, vector<ll>(gp.size() + 1, MAX));
        vector<vector<ll>> dpy2(rp.size() + 1, vector<ll>(gp.size() + 1, MAX));
        for(int r = 0; r <= i && r <= rp.size(); r++){
            for(int g = 0; r + g <= i && g <= gp.size(); g++){
                int y = i - r - g;
                if(y > yp.size()) continue;

                if(r)
                    dpr2[r][g] = min(dpg[r - 1][g] + max(0, gc[rp[r - 1]] - g) + max(0, yc[rp[r - 1]] - y),
                                     dpy[r - 1][g] + max(0, gc[rp[r - 1]] - g) + max(0, yc[rp[r - 1]] - y));
                if(g)
                    dpg2[r][g] = min(dpr[r][g - 1] + max(0, yc[gp[g - 1]] - y) + max(0, rc[gp[g - 1]] - r),
                                     dpy[r][g - 1] + max(0, yc[gp[g - 1]] - y) + max(0, rc[gp[g - 1]] - r));
                if(y)
                    dpy2[r][g] = min(dpr[r][g] + max(0, rc[yp[y - 1]] - r) + max(0, gc[yp[y - 1]] - g),
                                     dpg[r][g] + max(0, rc[yp[y - 1]] - r) + max(0, gc[yp[y - 1]] - g));
            }
        }
        dpr = dpr2;
        dpg = dpg2;
        dpy = dpy2;
    }

    if(min({dpr[rp.size()][gp.size()], dpg[rp.size()][gp.size()], dpy[rp.size()][gp.size()]}) >= MAX) cout << "-1\n";
    else cout << min({dpr[rp.size()][gp.size()], dpg[rp.size()][gp.size()], dpy[rp.size()][gp.size()]}) << "\n";

    return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:78:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int r = 0; r <= i && r <= rp.size(); r++){
                                  ~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:79:44: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int g = 0; r + g <= i && g <= gp.size(); g++){
                                          ~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:81:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if(y > yp.size()) continue;
                    ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 5 ms 384 KB Output is correct
35 Correct 5 ms 384 KB Output is correct
36 Correct 5 ms 384 KB Output is correct
37 Correct 5 ms 384 KB Output is correct
38 Correct 5 ms 384 KB Output is correct
39 Correct 5 ms 384 KB Output is correct
40 Correct 4 ms 384 KB Output is correct
41 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 178 ms 2844 KB Output is correct
3 Correct 180 ms 2632 KB Output is correct
4 Correct 172 ms 2332 KB Output is correct
5 Correct 175 ms 2736 KB Output is correct
6 Correct 170 ms 2708 KB Output is correct
7 Correct 180 ms 2820 KB Output is correct
8 Correct 174 ms 2752 KB Output is correct
9 Correct 168 ms 2324 KB Output is correct
10 Correct 179 ms 2724 KB Output is correct
11 Correct 169 ms 2332 KB Output is correct
12 Correct 24 ms 932 KB Output is correct
13 Correct 58 ms 1312 KB Output is correct
14 Correct 97 ms 1736 KB Output is correct
15 Correct 182 ms 2456 KB Output is correct
16 Correct 176 ms 2700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 5 ms 384 KB Output is correct
35 Correct 5 ms 384 KB Output is correct
36 Correct 5 ms 384 KB Output is correct
37 Correct 5 ms 384 KB Output is correct
38 Correct 5 ms 384 KB Output is correct
39 Correct 5 ms 384 KB Output is correct
40 Correct 4 ms 384 KB Output is correct
41 Correct 5 ms 384 KB Output is correct
42 Correct 5 ms 384 KB Output is correct
43 Correct 178 ms 2844 KB Output is correct
44 Correct 180 ms 2632 KB Output is correct
45 Correct 172 ms 2332 KB Output is correct
46 Correct 175 ms 2736 KB Output is correct
47 Correct 170 ms 2708 KB Output is correct
48 Correct 180 ms 2820 KB Output is correct
49 Correct 174 ms 2752 KB Output is correct
50 Correct 168 ms 2324 KB Output is correct
51 Correct 179 ms 2724 KB Output is correct
52 Correct 169 ms 2332 KB Output is correct
53 Correct 24 ms 932 KB Output is correct
54 Correct 58 ms 1312 KB Output is correct
55 Correct 97 ms 1736 KB Output is correct
56 Correct 182 ms 2456 KB Output is correct
57 Correct 176 ms 2700 KB Output is correct
58 Correct 116 ms 1200 KB Output is correct
59 Correct 133 ms 1372 KB Output is correct
60 Correct 123 ms 1384 KB Output is correct
61 Correct 125 ms 1292 KB Output is correct
62 Correct 177 ms 2304 KB Output is correct
63 Correct 170 ms 2300 KB Output is correct
64 Correct 172 ms 2032 KB Output is correct
65 Correct 159 ms 1820 KB Output is correct
66 Correct 125 ms 1264 KB Output is correct
67 Correct 119 ms 1276 KB Output is correct
68 Correct 125 ms 1280 KB Output is correct
69 Correct 127 ms 1280 KB Output is correct
70 Correct 128 ms 1536 KB Output is correct
71 Correct 120 ms 1240 KB Output is correct
72 Correct 59 ms 860 KB Output is correct
73 Correct 8 ms 384 KB Output is correct