# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1087628 | 2024-09-13T03:21:15 Z | efishel | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++17 | 500 ms | 769108 KB |
#include <bits/stdc++.h> using namespace std; using ll = long long; using vll = vector <ll>; vll ou[3]; int dp[403][403][403][3]; // dp[a][b][c][last] = min operations to have a valid string\ rn having a prefix of length a+b+c having taken a as, b bs, and c cs, having the last color as last int fposi (ll a, ll b, ll c, ll i) { int pos = i; for (ll j = 0; j < a; j++) if (ou[0][j] > i) pos++; for (ll j = 0; j < b; j++) if (ou[1][j] > i) pos++; for (ll j = 0; j < c; j++) if (ou[2][j] > i) pos++; return pos; } ll n; int solve (ll a, ll b, ll c, ll last) { // have taken the first a as b bs and c cs, the last rn is last if (a+b+c == n) return 0; if (dp[a][b][c][last] <= 1e9) return dp[a][b][c][last]; int &ans = dp[a][b][c][last]; ans = 1e9; int i = a+b+c; if (last != 0 && a < ou[0].size()) { ans = min(ans, solve(a+1, b, c, 0) + fposi(a, b, c, ou[0][a])-i); } if (last != 1 && b < ou[1].size()) { ans = min(ans, solve(a, b+1, c, 1) + fposi(a, b, c, ou[1][b])-i); } if (last != 2 && c < ou[2].size()) { ans = min(ans, solve(a, b, c+1, 2) + fposi(a, b, c, ou[2][c])-i); } return ans; } int main () { cin.tie(nullptr) -> sync_with_stdio(false); cin >> n; string str; cin >> str; memset(dp, 0x3f, sizeof(dp)); for (ll i = 0; i < n; i++) { ou[(str[i] == 'R' ? 0 : str[i] == 'G' ? 1 : 2)].push_back(i); } ll ans = min({ solve(0, 0, 0, 0), solve(0, 0, 0, 1), solve(0, 0, 0, 2) }); cout << (ans == 1e9 ? -1 : ans) << '\n'; return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 350 ms | 768928 KB | Output is correct |
2 | Correct | 375 ms | 768904 KB | Output is correct |
3 | Correct | 358 ms | 768776 KB | Output is correct |
4 | Correct | 344 ms | 768784 KB | Output is correct |
5 | Correct | 350 ms | 768792 KB | Output is correct |
6 | Correct | 364 ms | 768888 KB | Output is correct |
7 | Correct | 345 ms | 768848 KB | Output is correct |
8 | Correct | 356 ms | 768852 KB | Output is correct |
9 | Correct | 368 ms | 768848 KB | Output is correct |
10 | Correct | 369 ms | 768768 KB | Output is correct |
11 | Correct | 357 ms | 768848 KB | Output is correct |
12 | Correct | 353 ms | 768708 KB | Output is correct |
13 | Correct | 351 ms | 768844 KB | Output is correct |
14 | Correct | 345 ms | 768852 KB | Output is correct |
15 | Correct | 378 ms | 768852 KB | Output is correct |
16 | Correct | 374 ms | 768852 KB | Output is correct |
17 | Correct | 373 ms | 768848 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 350 ms | 768928 KB | Output is correct |
2 | Correct | 375 ms | 768904 KB | Output is correct |
3 | Correct | 358 ms | 768776 KB | Output is correct |
4 | Correct | 344 ms | 768784 KB | Output is correct |
5 | Correct | 350 ms | 768792 KB | Output is correct |
6 | Correct | 364 ms | 768888 KB | Output is correct |
7 | Correct | 345 ms | 768848 KB | Output is correct |
8 | Correct | 356 ms | 768852 KB | Output is correct |
9 | Correct | 368 ms | 768848 KB | Output is correct |
10 | Correct | 369 ms | 768768 KB | Output is correct |
11 | Correct | 357 ms | 768848 KB | Output is correct |
12 | Correct | 353 ms | 768708 KB | Output is correct |
13 | Correct | 351 ms | 768844 KB | Output is correct |
14 | Correct | 345 ms | 768852 KB | Output is correct |
15 | Correct | 378 ms | 768852 KB | Output is correct |
16 | Correct | 374 ms | 768852 KB | Output is correct |
17 | Correct | 373 ms | 768848 KB | Output is correct |
18 | Correct | 360 ms | 768784 KB | Output is correct |
19 | Correct | 382 ms | 768884 KB | Output is correct |
20 | Correct | 356 ms | 768844 KB | Output is correct |
21 | Correct | 355 ms | 768912 KB | Output is correct |
22 | Correct | 414 ms | 768852 KB | Output is correct |
23 | Correct | 356 ms | 768936 KB | Output is correct |
24 | Correct | 314 ms | 768852 KB | Output is correct |
25 | Correct | 375 ms | 768876 KB | Output is correct |
26 | Correct | 367 ms | 768920 KB | Output is correct |
27 | Correct | 359 ms | 768852 KB | Output is correct |
28 | Correct | 394 ms | 768836 KB | Output is correct |
29 | Correct | 354 ms | 768800 KB | Output is correct |
30 | Correct | 371 ms | 768848 KB | Output is correct |
31 | Correct | 373 ms | 768852 KB | Output is correct |
32 | Correct | 368 ms | 768752 KB | Output is correct |
33 | Correct | 350 ms | 768852 KB | Output is correct |
34 | Correct | 358 ms | 768768 KB | Output is correct |
35 | Correct | 351 ms | 768848 KB | Output is correct |
36 | Correct | 331 ms | 768872 KB | Output is correct |
37 | Correct | 319 ms | 768848 KB | Output is correct |
38 | Correct | 347 ms | 768792 KB | Output is correct |
39 | Correct | 330 ms | 768856 KB | Output is correct |
40 | Correct | 355 ms | 768848 KB | Output is correct |
41 | Correct | 384 ms | 768684 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 385 ms | 768928 KB | Output is correct |
2 | Correct | 373 ms | 768884 KB | Output is correct |
3 | Correct | 353 ms | 769108 KB | Output is correct |
4 | Correct | 364 ms | 768740 KB | Output is correct |
5 | Correct | 376 ms | 768848 KB | Output is correct |
6 | Correct | 351 ms | 768740 KB | Output is correct |
7 | Correct | 377 ms | 768860 KB | Output is correct |
8 | Correct | 353 ms | 768864 KB | Output is correct |
9 | Correct | 361 ms | 768748 KB | Output is correct |
10 | Correct | 371 ms | 768848 KB | Output is correct |
11 | Correct | 359 ms | 768760 KB | Output is correct |
12 | Correct | 355 ms | 769104 KB | Output is correct |
13 | Correct | 358 ms | 768948 KB | Output is correct |
14 | Correct | 357 ms | 768888 KB | Output is correct |
15 | Correct | 371 ms | 768772 KB | Output is correct |
16 | Correct | 360 ms | 768740 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 350 ms | 768928 KB | Output is correct |
2 | Correct | 375 ms | 768904 KB | Output is correct |
3 | Correct | 358 ms | 768776 KB | Output is correct |
4 | Correct | 344 ms | 768784 KB | Output is correct |
5 | Correct | 350 ms | 768792 KB | Output is correct |
6 | Correct | 364 ms | 768888 KB | Output is correct |
7 | Correct | 345 ms | 768848 KB | Output is correct |
8 | Correct | 356 ms | 768852 KB | Output is correct |
9 | Correct | 368 ms | 768848 KB | Output is correct |
10 | Correct | 369 ms | 768768 KB | Output is correct |
11 | Correct | 357 ms | 768848 KB | Output is correct |
12 | Correct | 353 ms | 768708 KB | Output is correct |
13 | Correct | 351 ms | 768844 KB | Output is correct |
14 | Correct | 345 ms | 768852 KB | Output is correct |
15 | Correct | 378 ms | 768852 KB | Output is correct |
16 | Correct | 374 ms | 768852 KB | Output is correct |
17 | Correct | 373 ms | 768848 KB | Output is correct |
18 | Correct | 360 ms | 768784 KB | Output is correct |
19 | Correct | 382 ms | 768884 KB | Output is correct |
20 | Correct | 356 ms | 768844 KB | Output is correct |
21 | Correct | 355 ms | 768912 KB | Output is correct |
22 | Correct | 414 ms | 768852 KB | Output is correct |
23 | Correct | 356 ms | 768936 KB | Output is correct |
24 | Correct | 314 ms | 768852 KB | Output is correct |
25 | Correct | 375 ms | 768876 KB | Output is correct |
26 | Correct | 367 ms | 768920 KB | Output is correct |
27 | Correct | 359 ms | 768852 KB | Output is correct |
28 | Correct | 394 ms | 768836 KB | Output is correct |
29 | Correct | 354 ms | 768800 KB | Output is correct |
30 | Correct | 371 ms | 768848 KB | Output is correct |
31 | Correct | 373 ms | 768852 KB | Output is correct |
32 | Correct | 368 ms | 768752 KB | Output is correct |
33 | Correct | 350 ms | 768852 KB | Output is correct |
34 | Correct | 358 ms | 768768 KB | Output is correct |
35 | Correct | 351 ms | 768848 KB | Output is correct |
36 | Correct | 331 ms | 768872 KB | Output is correct |
37 | Correct | 319 ms | 768848 KB | Output is correct |
38 | Correct | 347 ms | 768792 KB | Output is correct |
39 | Correct | 330 ms | 768856 KB | Output is correct |
40 | Correct | 355 ms | 768848 KB | Output is correct |
41 | Correct | 384 ms | 768684 KB | Output is correct |
42 | Correct | 385 ms | 768928 KB | Output is correct |
43 | Correct | 373 ms | 768884 KB | Output is correct |
44 | Correct | 353 ms | 769108 KB | Output is correct |
45 | Correct | 364 ms | 768740 KB | Output is correct |
46 | Correct | 376 ms | 768848 KB | Output is correct |
47 | Correct | 351 ms | 768740 KB | Output is correct |
48 | Correct | 377 ms | 768860 KB | Output is correct |
49 | Correct | 353 ms | 768864 KB | Output is correct |
50 | Correct | 361 ms | 768748 KB | Output is correct |
51 | Correct | 371 ms | 768848 KB | Output is correct |
52 | Correct | 359 ms | 768760 KB | Output is correct |
53 | Correct | 355 ms | 769104 KB | Output is correct |
54 | Correct | 358 ms | 768948 KB | Output is correct |
55 | Correct | 357 ms | 768888 KB | Output is correct |
56 | Correct | 371 ms | 768772 KB | Output is correct |
57 | Correct | 360 ms | 768740 KB | Output is correct |
58 | Execution timed out | 1088 ms | 768852 KB | Time limit exceeded |
59 | Halted | 0 ms | 0 KB | - |