Submission #871336

# Submission time Handle Problem Language Result Execution time Memory
871336 2023-11-10T15:14:16 Z hct_2so1 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
299 ms 757872 KB
#include <bits/stdc++.h>

#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define sz(v) ((ll) (v).size())
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define F first
#define S second
#define pii(x, y) make_pair(x, y)
#define __builtin_popcount __builtin_popcountll
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define lg(x) (63 - __builtin_clz(x))

template <class X, class Y>
    bool minimize(X &x, const Y &y)
    {
        if (x > y) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y)
    {
        if (x < y) {x = y; return 1;}
        return 0;
    }

using namespace std;
typedef long long ll;
const int N = 400 + 5;
const int M = 6e5;
const int mod = 998244353;
const int INF = 1e9 + 7;
const ll oo = 2e18;
const double eps = 1e-1;
const long double pi = 2*acos(0.0);

int n, cnt[N][3], mp[100];
vector<int> v[3];
string s;

void Input(void)
{
    mp['R'] = 0, mp['G'] = 1, mp['Y'] = 2;
    cin >> n >> s;
    s = ' ' + s;
    for (int i = 1; i <= n; i ++)
    {
        for (int j = 0; j < 3; j ++) cnt[i][j] = cnt[i - 1][j];
        cnt[i][mp[s[i]]] ++;
        v[mp[s[i]]].push_back(i);
    }
}

int dp[401][401][401][3];

int cost(int a, int b, int c, int pos)
{
    int res = 0;
    res += max(0, a - cnt[pos][0]);
    res += max(0, b - cnt[pos][1]);
    res += max(0, c - cnt[pos][2]);
    return res;
}

void solve(void)
{
    memset(dp, 0x3f, sizeof dp);
    for (int x = 0; x < 3; x ++) dp[0][0][0][x] = 0;
    int N0 = cnt[n][0], N1 = cnt[n][1], N2 = cnt[n][2];
    for (int i = 0; i <= N0; i ++)
        for (int j = 0; j <= N1; j ++)
            for (int k = 0; k <= N2; k ++)
                for (int x = 0; x < 3; x ++)
                    if (dp[i][j][k][x] < INF)
                    {
                        if (x != 0 && i < N0)
                            minimize(dp[i + 1][j][k][0], dp[i][j][k][x] + cost(i, j, k, v[0][i]));
                        if (x != 1 && j < N1)
                            minimize(dp[i][j + 1][k][1], dp[i][j][k][x] + cost(i, j, k, v[1][j]));
                        if (x != 2 && k < N2)
                            minimize(dp[i][j][k + 1][2], dp[i][j][k][x] + cost(i, j, k, v[2][k]));
                    }
    int ans = INF;
    for (int x = 0; x < 3; x ++) minimize(ans, dp[N0][N1][N2][x]);
    cout << (ans != INF ? ans : -1);
}

int main()
{
    //std::ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("mixpotions.inp", "r", stdin);
//freopen("mixpotions.out", "w", stdout);
    int test = 1;
    //cin >> test;
    while (test --)
    {
        Input();
        solve();
    }
    return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'void Input()':
joi2019_ho_t3.cpp:52:23: warning: array subscript has type 'char' [-Wchar-subscripts]
   52 |         cnt[i][mp[s[i]]] ++;
      |                       ^
joi2019_ho_t3.cpp:53:18: warning: array subscript has type 'char' [-Wchar-subscripts]
   53 |         v[mp[s[i]]].push_back(i);
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 299 ms 757504 KB Output is correct
2 Correct 235 ms 757412 KB Output is correct
3 Correct 238 ms 757356 KB Output is correct
4 Correct 227 ms 757328 KB Output is correct
5 Correct 226 ms 757336 KB Output is correct
6 Correct 231 ms 757300 KB Output is correct
7 Correct 222 ms 757304 KB Output is correct
8 Correct 246 ms 757336 KB Output is correct
9 Correct 228 ms 757332 KB Output is correct
10 Correct 225 ms 757328 KB Output is correct
11 Correct 226 ms 757336 KB Output is correct
12 Correct 224 ms 757328 KB Output is correct
13 Correct 223 ms 757332 KB Output is correct
14 Correct 222 ms 757584 KB Output is correct
15 Correct 222 ms 757328 KB Output is correct
16 Correct 223 ms 757332 KB Output is correct
17 Correct 226 ms 757488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 299 ms 757504 KB Output is correct
2 Correct 235 ms 757412 KB Output is correct
3 Correct 238 ms 757356 KB Output is correct
4 Correct 227 ms 757328 KB Output is correct
5 Correct 226 ms 757336 KB Output is correct
6 Correct 231 ms 757300 KB Output is correct
7 Correct 222 ms 757304 KB Output is correct
8 Correct 246 ms 757336 KB Output is correct
9 Correct 228 ms 757332 KB Output is correct
10 Correct 225 ms 757328 KB Output is correct
11 Correct 226 ms 757336 KB Output is correct
12 Correct 224 ms 757328 KB Output is correct
13 Correct 223 ms 757332 KB Output is correct
14 Correct 222 ms 757584 KB Output is correct
15 Correct 222 ms 757328 KB Output is correct
16 Correct 223 ms 757332 KB Output is correct
17 Correct 226 ms 757488 KB Output is correct
18 Correct 227 ms 757376 KB Output is correct
19 Correct 229 ms 757424 KB Output is correct
20 Correct 226 ms 757392 KB Output is correct
21 Correct 232 ms 757432 KB Output is correct
22 Correct 227 ms 757372 KB Output is correct
23 Correct 222 ms 757460 KB Output is correct
24 Correct 225 ms 757524 KB Output is correct
25 Correct 232 ms 757844 KB Output is correct
26 Correct 224 ms 757368 KB Output is correct
27 Correct 231 ms 757496 KB Output is correct
28 Correct 230 ms 757428 KB Output is correct
29 Correct 223 ms 757328 KB Output is correct
30 Correct 226 ms 757304 KB Output is correct
31 Correct 230 ms 757336 KB Output is correct
32 Correct 225 ms 757332 KB Output is correct
33 Correct 230 ms 757384 KB Output is correct
34 Correct 227 ms 757492 KB Output is correct
35 Correct 227 ms 757336 KB Output is correct
36 Correct 227 ms 757448 KB Output is correct
37 Correct 230 ms 757380 KB Output is correct
38 Correct 226 ms 757340 KB Output is correct
39 Correct 226 ms 757520 KB Output is correct
40 Correct 226 ms 757328 KB Output is correct
41 Correct 231 ms 757328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 232 ms 757532 KB Output is correct
2 Correct 227 ms 757524 KB Output is correct
3 Correct 243 ms 757332 KB Output is correct
4 Correct 230 ms 757576 KB Output is correct
5 Correct 229 ms 757296 KB Output is correct
6 Correct 228 ms 757332 KB Output is correct
7 Correct 231 ms 757408 KB Output is correct
8 Correct 230 ms 757304 KB Output is correct
9 Correct 230 ms 757720 KB Output is correct
10 Correct 225 ms 757332 KB Output is correct
11 Correct 225 ms 757368 KB Output is correct
12 Correct 227 ms 757592 KB Output is correct
13 Correct 242 ms 757332 KB Output is correct
14 Correct 226 ms 757328 KB Output is correct
15 Correct 248 ms 757332 KB Output is correct
16 Correct 242 ms 757328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 299 ms 757504 KB Output is correct
2 Correct 235 ms 757412 KB Output is correct
3 Correct 238 ms 757356 KB Output is correct
4 Correct 227 ms 757328 KB Output is correct
5 Correct 226 ms 757336 KB Output is correct
6 Correct 231 ms 757300 KB Output is correct
7 Correct 222 ms 757304 KB Output is correct
8 Correct 246 ms 757336 KB Output is correct
9 Correct 228 ms 757332 KB Output is correct
10 Correct 225 ms 757328 KB Output is correct
11 Correct 226 ms 757336 KB Output is correct
12 Correct 224 ms 757328 KB Output is correct
13 Correct 223 ms 757332 KB Output is correct
14 Correct 222 ms 757584 KB Output is correct
15 Correct 222 ms 757328 KB Output is correct
16 Correct 223 ms 757332 KB Output is correct
17 Correct 226 ms 757488 KB Output is correct
18 Correct 227 ms 757376 KB Output is correct
19 Correct 229 ms 757424 KB Output is correct
20 Correct 226 ms 757392 KB Output is correct
21 Correct 232 ms 757432 KB Output is correct
22 Correct 227 ms 757372 KB Output is correct
23 Correct 222 ms 757460 KB Output is correct
24 Correct 225 ms 757524 KB Output is correct
25 Correct 232 ms 757844 KB Output is correct
26 Correct 224 ms 757368 KB Output is correct
27 Correct 231 ms 757496 KB Output is correct
28 Correct 230 ms 757428 KB Output is correct
29 Correct 223 ms 757328 KB Output is correct
30 Correct 226 ms 757304 KB Output is correct
31 Correct 230 ms 757336 KB Output is correct
32 Correct 225 ms 757332 KB Output is correct
33 Correct 230 ms 757384 KB Output is correct
34 Correct 227 ms 757492 KB Output is correct
35 Correct 227 ms 757336 KB Output is correct
36 Correct 227 ms 757448 KB Output is correct
37 Correct 230 ms 757380 KB Output is correct
38 Correct 226 ms 757340 KB Output is correct
39 Correct 226 ms 757520 KB Output is correct
40 Correct 226 ms 757328 KB Output is correct
41 Correct 231 ms 757328 KB Output is correct
42 Correct 232 ms 757532 KB Output is correct
43 Correct 227 ms 757524 KB Output is correct
44 Correct 243 ms 757332 KB Output is correct
45 Correct 230 ms 757576 KB Output is correct
46 Correct 229 ms 757296 KB Output is correct
47 Correct 228 ms 757332 KB Output is correct
48 Correct 231 ms 757408 KB Output is correct
49 Correct 230 ms 757304 KB Output is correct
50 Correct 230 ms 757720 KB Output is correct
51 Correct 225 ms 757332 KB Output is correct
52 Correct 225 ms 757368 KB Output is correct
53 Correct 227 ms 757592 KB Output is correct
54 Correct 242 ms 757332 KB Output is correct
55 Correct 226 ms 757328 KB Output is correct
56 Correct 248 ms 757332 KB Output is correct
57 Correct 242 ms 757328 KB Output is correct
58 Correct 262 ms 757464 KB Output is correct
59 Correct 255 ms 757328 KB Output is correct
60 Correct 254 ms 757328 KB Output is correct
61 Correct 256 ms 757412 KB Output is correct
62 Correct 234 ms 757384 KB Output is correct
63 Correct 239 ms 757700 KB Output is correct
64 Correct 238 ms 757536 KB Output is correct
65 Correct 248 ms 757332 KB Output is correct
66 Correct 260 ms 757872 KB Output is correct
67 Correct 252 ms 757536 KB Output is correct
68 Correct 256 ms 757376 KB Output is correct
69 Correct 255 ms 757332 KB Output is correct
70 Correct 256 ms 757332 KB Output is correct
71 Correct 258 ms 757648 KB Output is correct
72 Correct 230 ms 757328 KB Output is correct
73 Correct 228 ms 757304 KB Output is correct