Submission #256400

# Submission time Handle Problem Language Result Execution time Memory
256400 2020-08-02T16:12:40 Z Osama_Alkhodairy Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
75 / 100
500 ms 757752 KB
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

const int N = 401;

int n, dp[N][N][N][3], sum[N][3], pos[N][3];
string s;

int get_sum(int idx, int l, int r){
    if(r < l || l == -1 || r == -1) return 0;
    return sum[r][idx] - (l == 0 ? 0 : sum[l - 1][idx]);
}
bool check(int a, int b, int c){
    if(a > c) swap(a, c);
    if(b > c) swap(b, c);
    if(a > b) swap(a, b);
    if(a + b + 1 < c) return 0;
    return 1;
}
int solve(int a, int b, int c, int las){
    if(!check(a, b, c)) return 1e9;
    if(a + b + c == n) return 0;
    int &ret = dp[a][b][c][las];
    if(ret != -1) return ret;
    ret = 1e9;
    if((las != 0 || a + b + c == 0) && pos[a][0] != -1){
        ret = min(ret, solve(a + 1, b, c, 0) + get_sum(1, pos[b][1], pos[a][0]) + get_sum(2, pos[c][2], pos[a][0]));
    }
    if((las != 1 || a + b + c == 0) && pos[b][1] != -1){
        ret = min(ret, solve(a, b + 1, c, 1) + get_sum(0, pos[a][0], pos[b][1]) + get_sum(2, pos[c][2], pos[b][1]));
    }
    if((las != 2 || a + b + c == 0) && pos[c][2] != -1){
        ret = min(ret, solve(a, b, c + 1, 2) + get_sum(0, pos[a][0], pos[c][2]) + get_sum(1, pos[b][1], pos[c][2]));
    }
    return ret;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    memset(pos, -1, sizeof pos);
    memset(dp, -1, sizeof dp);
    cin >> n >> s;
    for(int i = 0 ; i < n ; i++){
        if(s[i] == 'R') s[i] = 0;
        else if(s[i] == 'G') s[i] = 1;
        else s[i] = 2;
        for(int j = 0 ; j < 3 ; j++){
            if(i > 0) sum[i][j] = sum[i - 1][j];
            sum[i][j] += s[i] == j;
        }
    }
    int pt0 = 0, pt1 = 0, pt2 = 0;
    for(int i = 0 ; i < n ; i++){
        if(s[i] == 0) pos[pt0++][0] = i;
        else if(s[i] == 1) pos[pt1++][1] = i;
        else pos[pt2++][2] = i;
    }
    int ans = solve(0, 0, 0, 0);
    if(ans == 1e9) ans = -1;
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 392 ms 757392 KB Output is correct
2 Correct 382 ms 757496 KB Output is correct
3 Correct 399 ms 757496 KB Output is correct
4 Correct 395 ms 757496 KB Output is correct
5 Correct 372 ms 757460 KB Output is correct
6 Correct 381 ms 757496 KB Output is correct
7 Correct 376 ms 757496 KB Output is correct
8 Correct 376 ms 757496 KB Output is correct
9 Correct 377 ms 757624 KB Output is correct
10 Correct 379 ms 757496 KB Output is correct
11 Correct 373 ms 757500 KB Output is correct
12 Correct 373 ms 757496 KB Output is correct
13 Correct 399 ms 757496 KB Output is correct
14 Correct 381 ms 757624 KB Output is correct
15 Correct 387 ms 757620 KB Output is correct
16 Correct 413 ms 757496 KB Output is correct
17 Correct 388 ms 757496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 392 ms 757392 KB Output is correct
2 Correct 382 ms 757496 KB Output is correct
3 Correct 399 ms 757496 KB Output is correct
4 Correct 395 ms 757496 KB Output is correct
5 Correct 372 ms 757460 KB Output is correct
6 Correct 381 ms 757496 KB Output is correct
7 Correct 376 ms 757496 KB Output is correct
8 Correct 376 ms 757496 KB Output is correct
9 Correct 377 ms 757624 KB Output is correct
10 Correct 379 ms 757496 KB Output is correct
11 Correct 373 ms 757500 KB Output is correct
12 Correct 373 ms 757496 KB Output is correct
13 Correct 399 ms 757496 KB Output is correct
14 Correct 381 ms 757624 KB Output is correct
15 Correct 387 ms 757620 KB Output is correct
16 Correct 413 ms 757496 KB Output is correct
17 Correct 388 ms 757496 KB Output is correct
18 Correct 386 ms 757496 KB Output is correct
19 Correct 381 ms 757496 KB Output is correct
20 Correct 379 ms 757472 KB Output is correct
21 Correct 390 ms 757528 KB Output is correct
22 Correct 399 ms 757496 KB Output is correct
23 Correct 398 ms 757624 KB Output is correct
24 Correct 387 ms 757496 KB Output is correct
25 Correct 377 ms 757492 KB Output is correct
26 Correct 379 ms 757528 KB Output is correct
27 Correct 380 ms 757496 KB Output is correct
28 Correct 393 ms 757668 KB Output is correct
29 Correct 395 ms 757496 KB Output is correct
30 Correct 385 ms 757496 KB Output is correct
31 Correct 400 ms 757576 KB Output is correct
32 Correct 379 ms 757496 KB Output is correct
33 Correct 384 ms 757496 KB Output is correct
34 Correct 376 ms 757676 KB Output is correct
35 Correct 381 ms 757500 KB Output is correct
36 Correct 375 ms 757488 KB Output is correct
37 Correct 386 ms 757368 KB Output is correct
38 Correct 374 ms 757496 KB Output is correct
39 Correct 382 ms 757496 KB Output is correct
40 Correct 376 ms 757624 KB Output is correct
41 Correct 379 ms 757464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 408 ms 757496 KB Output is correct
2 Correct 374 ms 757476 KB Output is correct
3 Correct 376 ms 757752 KB Output is correct
4 Correct 375 ms 757500 KB Output is correct
5 Correct 410 ms 757496 KB Output is correct
6 Correct 406 ms 757496 KB Output is correct
7 Correct 398 ms 757496 KB Output is correct
8 Correct 459 ms 757496 KB Output is correct
9 Correct 403 ms 757496 KB Output is correct
10 Correct 409 ms 757496 KB Output is correct
11 Correct 382 ms 757496 KB Output is correct
12 Correct 380 ms 757500 KB Output is correct
13 Correct 407 ms 757632 KB Output is correct
14 Correct 377 ms 757544 KB Output is correct
15 Correct 381 ms 757496 KB Output is correct
16 Correct 396 ms 757480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 392 ms 757392 KB Output is correct
2 Correct 382 ms 757496 KB Output is correct
3 Correct 399 ms 757496 KB Output is correct
4 Correct 395 ms 757496 KB Output is correct
5 Correct 372 ms 757460 KB Output is correct
6 Correct 381 ms 757496 KB Output is correct
7 Correct 376 ms 757496 KB Output is correct
8 Correct 376 ms 757496 KB Output is correct
9 Correct 377 ms 757624 KB Output is correct
10 Correct 379 ms 757496 KB Output is correct
11 Correct 373 ms 757500 KB Output is correct
12 Correct 373 ms 757496 KB Output is correct
13 Correct 399 ms 757496 KB Output is correct
14 Correct 381 ms 757624 KB Output is correct
15 Correct 387 ms 757620 KB Output is correct
16 Correct 413 ms 757496 KB Output is correct
17 Correct 388 ms 757496 KB Output is correct
18 Correct 386 ms 757496 KB Output is correct
19 Correct 381 ms 757496 KB Output is correct
20 Correct 379 ms 757472 KB Output is correct
21 Correct 390 ms 757528 KB Output is correct
22 Correct 399 ms 757496 KB Output is correct
23 Correct 398 ms 757624 KB Output is correct
24 Correct 387 ms 757496 KB Output is correct
25 Correct 377 ms 757492 KB Output is correct
26 Correct 379 ms 757528 KB Output is correct
27 Correct 380 ms 757496 KB Output is correct
28 Correct 393 ms 757668 KB Output is correct
29 Correct 395 ms 757496 KB Output is correct
30 Correct 385 ms 757496 KB Output is correct
31 Correct 400 ms 757576 KB Output is correct
32 Correct 379 ms 757496 KB Output is correct
33 Correct 384 ms 757496 KB Output is correct
34 Correct 376 ms 757676 KB Output is correct
35 Correct 381 ms 757500 KB Output is correct
36 Correct 375 ms 757488 KB Output is correct
37 Correct 386 ms 757368 KB Output is correct
38 Correct 374 ms 757496 KB Output is correct
39 Correct 382 ms 757496 KB Output is correct
40 Correct 376 ms 757624 KB Output is correct
41 Correct 379 ms 757464 KB Output is correct
42 Correct 408 ms 757496 KB Output is correct
43 Correct 374 ms 757476 KB Output is correct
44 Correct 376 ms 757752 KB Output is correct
45 Correct 375 ms 757500 KB Output is correct
46 Correct 410 ms 757496 KB Output is correct
47 Correct 406 ms 757496 KB Output is correct
48 Correct 398 ms 757496 KB Output is correct
49 Correct 459 ms 757496 KB Output is correct
50 Correct 403 ms 757496 KB Output is correct
51 Correct 409 ms 757496 KB Output is correct
52 Correct 382 ms 757496 KB Output is correct
53 Correct 380 ms 757500 KB Output is correct
54 Correct 407 ms 757632 KB Output is correct
55 Correct 377 ms 757544 KB Output is correct
56 Correct 381 ms 757496 KB Output is correct
57 Correct 396 ms 757480 KB Output is correct
58 Execution timed out 573 ms 757496 KB Time limit exceeded
59 Halted 0 ms 0 KB -