Submission #481427

# Submission time Handle Problem Language Result Execution time Memory
481427 2021-10-20T19:05:41 Z Lobo Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
75 / 100
500 ms 1009924 KB
#include <bits/stdc++.h>
 
using namespace std;

const long long INFll = (long long) 1e18 + 10;
const int INFii = (int) 1e9 + 10;
typedef long long ll;
typedef int ii;
typedef long double dbl;
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

#define maxn 401

ii n, dp[maxn][maxn][maxn][4];
ii qtd1, qtd2, qtd3;
ii pos1[maxn], pos2[maxn], pos3[maxn];
ii cnt1[maxn], cnt2[maxn], cnt3[maxn];
string s;

ii sol(ii c1, ii c2, ii c3, ii ant) {
    if(dp[c1][c2][c3][ant] != -1) return dp[c1][c2][c3][ant];

    ii ans = INFii;

    if(c1 != qtd1 && ant != 1) {
        ans = min(ans, sol(c1+1,c2,c3,1) + max(0,cnt2[pos2[c2]]-cnt2[pos1[c1+1]]) + max(0,cnt3[pos3[c3]]-cnt3[pos1[c1+1]]));
    }

    if(c2 != qtd2 && ant != 2) {
        ans = min(ans, sol(c1,c2+1,c3,2) + max(0,cnt1[pos1[c1]]-cnt1[pos2[c2+1]]) + max(0,cnt3[pos3[c3]]-cnt3[pos2[c2+1]]));
    }

    if(c3 != qtd3 && ant != 3) {
        ans = min(ans, sol(c1,c2,c3+1,3) + max(0,cnt2[pos2[c2]]-cnt2[pos3[c3+1]]) + max(0,cnt1[pos1[c1]]-cnt1[pos3[c3+1]]));
    }

    return dp[c1][c2][c3][ant] = ans;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);

    //freopen("in.in", "r", stdin);
    //freopen("out.out", "w", stdout);

    cin >> n >> s;

    for(ii i = 1; i <= n; i++) {
        cnt1[i] = cnt1[i-1];
        cnt2[i] = cnt2[i-1];
        cnt3[i] = cnt3[i-1];

        if(s[i-1] == 'R') {
            cnt1[i]++;
            pos1[++qtd1] = i;
        }
        if(s[i-1] == 'G') {
            cnt2[i]++;
            pos2[++qtd2] = i;
        }
        if(s[i-1] == 'Y') {
            cnt3[i]++;
            pos3[++qtd3] = i;
        }
    }

    for(ii i = 0; i <= n; i++) {
        for(ii j = 0; j <= n; j++) {
            for(ii k = 0; k <= n; k++) {
                dp[i][j][k][0] = dp[i][j][k][1] = dp[i][j][k][2] = dp[i][j][k][3] = -1;
            }
        }
    }

    if(qtd1 > (n+1)/2 || qtd2 > (n+1)/2 || qtd3 > (n+1)/2) {
        cout << -1 << endl;
        return 0;
    }
    
    dp[qtd1][qtd2][qtd3][0] = dp[qtd1][qtd2][qtd3][1] = dp[qtd1][qtd2][qtd3][2] = dp[qtd1][qtd2][qtd3][3]  = 0;
    cout << sol(0,0,0,0) << endl;

}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 844 KB Output is correct
5 Correct 1 ms 1472 KB Output is correct
6 Correct 1 ms 1356 KB Output is correct
7 Correct 1 ms 1356 KB Output is correct
8 Correct 1 ms 1356 KB Output is correct
9 Correct 1 ms 1356 KB Output is correct
10 Correct 1 ms 1356 KB Output is correct
11 Correct 1 ms 1356 KB Output is correct
12 Correct 1 ms 1376 KB Output is correct
13 Correct 1 ms 1356 KB Output is correct
14 Correct 1 ms 1228 KB Output is correct
15 Correct 1 ms 1356 KB Output is correct
16 Correct 1 ms 1356 KB Output is correct
17 Correct 1 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 844 KB Output is correct
5 Correct 1 ms 1472 KB Output is correct
6 Correct 1 ms 1356 KB Output is correct
7 Correct 1 ms 1356 KB Output is correct
8 Correct 1 ms 1356 KB Output is correct
9 Correct 1 ms 1356 KB Output is correct
10 Correct 1 ms 1356 KB Output is correct
11 Correct 1 ms 1356 KB Output is correct
12 Correct 1 ms 1376 KB Output is correct
13 Correct 1 ms 1356 KB Output is correct
14 Correct 1 ms 1228 KB Output is correct
15 Correct 1 ms 1356 KB Output is correct
16 Correct 1 ms 1356 KB Output is correct
17 Correct 1 ms 1228 KB Output is correct
18 Correct 9 ms 18892 KB Output is correct
19 Correct 8 ms 18892 KB Output is correct
20 Correct 9 ms 18892 KB Output is correct
21 Correct 8 ms 18892 KB Output is correct
22 Correct 9 ms 18892 KB Output is correct
23 Correct 8 ms 18892 KB Output is correct
24 Correct 8 ms 18916 KB Output is correct
25 Correct 8 ms 18892 KB Output is correct
26 Correct 8 ms 18892 KB Output is correct
27 Correct 8 ms 18892 KB Output is correct
28 Correct 8 ms 18912 KB Output is correct
29 Correct 8 ms 18868 KB Output is correct
30 Correct 8 ms 18892 KB Output is correct
31 Correct 8 ms 18892 KB Output is correct
32 Correct 8 ms 18892 KB Output is correct
33 Correct 7 ms 18320 KB Output is correct
34 Correct 8 ms 18252 KB Output is correct
35 Correct 8 ms 17612 KB Output is correct
36 Correct 8 ms 18244 KB Output is correct
37 Correct 9 ms 16932 KB Output is correct
38 Correct 9 ms 18976 KB Output is correct
39 Correct 9 ms 18904 KB Output is correct
40 Correct 8 ms 18288 KB Output is correct
41 Correct 10 ms 19004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 475 ms 1009768 KB Output is correct
3 Correct 449 ms 1006440 KB Output is correct
4 Correct 429 ms 1009744 KB Output is correct
5 Correct 467 ms 1009824 KB Output is correct
6 Correct 432 ms 1009732 KB Output is correct
7 Correct 433 ms 1006376 KB Output is correct
8 Correct 433 ms 1006272 KB Output is correct
9 Correct 432 ms 1001324 KB Output is correct
10 Correct 437 ms 1009704 KB Output is correct
11 Correct 431 ms 1009760 KB Output is correct
12 Correct 114 ms 269912 KB Output is correct
13 Correct 195 ms 476068 KB Output is correct
14 Correct 291 ms 689348 KB Output is correct
15 Correct 430 ms 1009724 KB Output is correct
16 Correct 445 ms 1009860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 844 KB Output is correct
5 Correct 1 ms 1472 KB Output is correct
6 Correct 1 ms 1356 KB Output is correct
7 Correct 1 ms 1356 KB Output is correct
8 Correct 1 ms 1356 KB Output is correct
9 Correct 1 ms 1356 KB Output is correct
10 Correct 1 ms 1356 KB Output is correct
11 Correct 1 ms 1356 KB Output is correct
12 Correct 1 ms 1376 KB Output is correct
13 Correct 1 ms 1356 KB Output is correct
14 Correct 1 ms 1228 KB Output is correct
15 Correct 1 ms 1356 KB Output is correct
16 Correct 1 ms 1356 KB Output is correct
17 Correct 1 ms 1228 KB Output is correct
18 Correct 9 ms 18892 KB Output is correct
19 Correct 8 ms 18892 KB Output is correct
20 Correct 9 ms 18892 KB Output is correct
21 Correct 8 ms 18892 KB Output is correct
22 Correct 9 ms 18892 KB Output is correct
23 Correct 8 ms 18892 KB Output is correct
24 Correct 8 ms 18916 KB Output is correct
25 Correct 8 ms 18892 KB Output is correct
26 Correct 8 ms 18892 KB Output is correct
27 Correct 8 ms 18892 KB Output is correct
28 Correct 8 ms 18912 KB Output is correct
29 Correct 8 ms 18868 KB Output is correct
30 Correct 8 ms 18892 KB Output is correct
31 Correct 8 ms 18892 KB Output is correct
32 Correct 8 ms 18892 KB Output is correct
33 Correct 7 ms 18320 KB Output is correct
34 Correct 8 ms 18252 KB Output is correct
35 Correct 8 ms 17612 KB Output is correct
36 Correct 8 ms 18244 KB Output is correct
37 Correct 9 ms 16932 KB Output is correct
38 Correct 9 ms 18976 KB Output is correct
39 Correct 9 ms 18904 KB Output is correct
40 Correct 8 ms 18288 KB Output is correct
41 Correct 10 ms 19004 KB Output is correct
42 Correct 0 ms 332 KB Output is correct
43 Correct 475 ms 1009768 KB Output is correct
44 Correct 449 ms 1006440 KB Output is correct
45 Correct 429 ms 1009744 KB Output is correct
46 Correct 467 ms 1009824 KB Output is correct
47 Correct 432 ms 1009732 KB Output is correct
48 Correct 433 ms 1006376 KB Output is correct
49 Correct 433 ms 1006272 KB Output is correct
50 Correct 432 ms 1001324 KB Output is correct
51 Correct 437 ms 1009704 KB Output is correct
52 Correct 431 ms 1009760 KB Output is correct
53 Correct 114 ms 269912 KB Output is correct
54 Correct 195 ms 476068 KB Output is correct
55 Correct 291 ms 689348 KB Output is correct
56 Correct 430 ms 1009724 KB Output is correct
57 Correct 445 ms 1009860 KB Output is correct
58 Execution timed out 544 ms 1009924 KB Time limit exceeded
59 Halted 0 ms 0 KB -