Submission #590007

# Submission time Handle Problem Language Result Execution time Memory
590007 2022-07-05T13:03:49 Z boris_mihov Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
451 ms 130112 KB
#include <algorithm>
#include <iostream>
#include <vector>

typedef long long llong;
const int MAXN = 400 + 10;
const int INF = 1e9;

int n;
char s[MAXN];
int dp[MAXN][MAXN][MAXN][4];
bool bl[MAXN][MAXN][MAXN][4];
int byPos[4][MAXN];
int len[4];

int binary(int a[], int bound, int border)
{
    int l = -1, r = bound, mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        if (a[mid] <= border) l = mid;
        else r = mid;
    }

    return bound - 1 - l;
}

int f(int ones, int twos, int threes, int last)
{
    if (ones + twos + threes == n) return 0;
    if (bl[ones][twos][threes][last]) return dp[ones][twos][threes][last];
    
    bl[ones][twos][threes][last] = true;
    dp[ones][twos][threes][last] = INF;

    if (last != 1 && ones != len[1])
    {
        int add = binary(byPos[2], twos, byPos[1][ones]) + binary(byPos[3], threes, byPos[1][ones]);
        dp[ones][twos][threes][last] = std::min(dp[ones][twos][threes][last], f(ones + 1, twos, threes, 1) + add + byPos[1][ones] - (ones + twos + threes + 1));
    }

    if (last != 2 && twos != len[2])
    {
        int add = binary(byPos[1], ones, byPos[2][twos]) + binary(byPos[3], threes, byPos[2][twos]);
        dp[ones][twos][threes][last] = std::min(dp[ones][twos][threes][last], f(ones, twos + 1, threes, 2) + add + byPos[2][twos] - (ones + twos + threes + 1));
    }

    if (last != 3 && threes != len[3])
    {
        int add = binary(byPos[1], ones, byPos[3][threes]) + binary(byPos[2], twos, byPos[3][threes]);
        dp[ones][twos][threes][last] = std::min(dp[ones][twos][threes][last], f(ones, twos, threes + 1, 3) + add + byPos[3][threes] - (ones + twos + threes + 1));
    }

    return dp[ones][twos][threes][last];
}

void solve()
{
    for (int i = 1 ; i <= n ; ++i)
    {
        if (s[i] == 'R') s[i] = '1';
        if (s[i] == 'G') s[i] = '2';
        if (s[i] == 'Y') s[i] = '3';
        byPos[s[i]-'0'][len[s[i]-'0']++] = i;
    }

    int result = f(0, 0, 0, 0);
    if (result == INF) result = -1;
    std::cout << result << '\n';
}

void read()
{
    std::cin >> n;
    std::cin >> s + 1;
}

void fastIO()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main ()
{
    fastIO();
    read();
    solve();

    return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'void read()':
joi2019_ho_t3.cpp:76:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   76 |     std::cin >> s + 1;
      |                 ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 3 ms 3028 KB Output is correct
19 Correct 2 ms 2516 KB Output is correct
20 Correct 3 ms 2900 KB Output is correct
21 Correct 3 ms 3156 KB Output is correct
22 Correct 2 ms 2132 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 2 ms 2004 KB Output is correct
25 Correct 1 ms 1108 KB Output is correct
26 Correct 1 ms 1236 KB Output is correct
27 Correct 3 ms 3156 KB Output is correct
28 Correct 2 ms 3156 KB Output is correct
29 Correct 3 ms 2900 KB Output is correct
30 Correct 2 ms 2900 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 3028 KB Output is correct
33 Correct 1 ms 1492 KB Output is correct
34 Correct 1 ms 1748 KB Output is correct
35 Correct 3 ms 3156 KB Output is correct
36 Correct 2 ms 2516 KB Output is correct
37 Correct 2 ms 2260 KB Output is correct
38 Correct 3 ms 2644 KB Output is correct
39 Correct 3 ms 3028 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 2 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 2 ms 5204 KB Output is correct
8 Correct 2 ms 5204 KB Output is correct
9 Correct 2 ms 5204 KB Output is correct
10 Correct 3 ms 5204 KB Output is correct
11 Correct 3 ms 5228 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 3668 KB Output is correct
14 Correct 3 ms 4308 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 2 ms 5144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 3 ms 3028 KB Output is correct
19 Correct 2 ms 2516 KB Output is correct
20 Correct 3 ms 2900 KB Output is correct
21 Correct 3 ms 3156 KB Output is correct
22 Correct 2 ms 2132 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 2 ms 2004 KB Output is correct
25 Correct 1 ms 1108 KB Output is correct
26 Correct 1 ms 1236 KB Output is correct
27 Correct 3 ms 3156 KB Output is correct
28 Correct 2 ms 3156 KB Output is correct
29 Correct 3 ms 2900 KB Output is correct
30 Correct 2 ms 2900 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 3028 KB Output is correct
33 Correct 1 ms 1492 KB Output is correct
34 Correct 1 ms 1748 KB Output is correct
35 Correct 3 ms 3156 KB Output is correct
36 Correct 2 ms 2516 KB Output is correct
37 Correct 2 ms 2260 KB Output is correct
38 Correct 3 ms 2644 KB Output is correct
39 Correct 3 ms 3028 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 980 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 3 ms 5204 KB Output is correct
44 Correct 3 ms 5204 KB Output is correct
45 Correct 3 ms 5204 KB Output is correct
46 Correct 2 ms 5204 KB Output is correct
47 Correct 3 ms 5204 KB Output is correct
48 Correct 2 ms 5204 KB Output is correct
49 Correct 2 ms 5204 KB Output is correct
50 Correct 2 ms 5204 KB Output is correct
51 Correct 3 ms 5204 KB Output is correct
52 Correct 3 ms 5228 KB Output is correct
53 Correct 2 ms 2772 KB Output is correct
54 Correct 2 ms 3668 KB Output is correct
55 Correct 3 ms 4308 KB Output is correct
56 Correct 3 ms 5204 KB Output is correct
57 Correct 2 ms 5144 KB Output is correct
58 Correct 388 ms 108776 KB Output is correct
59 Correct 427 ms 130112 KB Output is correct
60 Correct 402 ms 122184 KB Output is correct
61 Correct 451 ms 119492 KB Output is correct
62 Correct 10 ms 13012 KB Output is correct
63 Correct 13 ms 22484 KB Output is correct
64 Correct 76 ms 71148 KB Output is correct
65 Correct 170 ms 100784 KB Output is correct
66 Correct 326 ms 120552 KB Output is correct
67 Correct 334 ms 115208 KB Output is correct
68 Correct 304 ms 123796 KB Output is correct
69 Correct 310 ms 120320 KB Output is correct
70 Correct 328 ms 127564 KB Output is correct
71 Correct 394 ms 116552 KB Output is correct
72 Correct 20 ms 15504 KB Output is correct
73 Correct 3 ms 3412 KB Output is correct