답안 #1107369

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1107369 2024-11-01T07:15:27 Z vjudge1 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
75 / 100
500 ms 774708 KB
# include <bits/stdc++.h>
using namespace std;
 
# define ll long long
# define pii pair <int, int>
# define pll pair <ll, ll>
# define nl '\n'
# define sz(x) (int)(x).size()
# define all(x) (x).begin(), (x).end()
# define deb(x) cerr << #x << " = " << x << endl;
 
const int maxn = (int)404;
const ll inf = (ll)1e9 + 0;
const int mod = (int)1e9 + 7;
const bool T = 0;

int n;
string s;
int dp[maxn][maxn][maxn][3];
int pref[3][maxn];
vector <int> g[3];
int d[3];

int f (char c) {
    if (c == 'R') return 0;
    if (c == 'G') return 1;
    if (c == 'Y') return 2;
    assert(0);
    return -1;
}

void op (int &a, int b) {
    a = min(a, b);
}

void ma1n () {
    cin >> n >> s;
    g[0].push_back(0);
    g[1].push_back(0);
    g[2].push_back(0);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 3; ++j) {
            pref[j][i + 1] = pref[j][i];
        }
        int x = f(s[i]);
        pref[x][i + 1]++;
        g[x].push_back(i + 1);
    }
    memset(dp, 0x3f, sizeof dp);
    int lim = dp[3][2][1][0];
    dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
    // deb(s);
    // exit(0);
    for (int i = 0; i < n; ++i) {
        for (int x = 0; x <= min(i, pref[0][n]); ++x) {
            for (int y = 0; y <= min(i - x, pref[1][n]); ++y) {
                if (x + y > i) continue;
                int z = i - x - y;
                if (z > pref[2][n]) continue;
                d[0] = x, d[1] = y, d[2] = z;
                for (int prv = 0; prv < 3; ++prv) {
                    for (int cur = 0; cur < 3; ++cur) {
                        if (prv == cur || d[cur] == pref[cur][n]) continue;
                        // cout << "continue everything" << nl;
                        // continue;
                        int othr = prv ^ cur ^ 3;
                        assert(othr >= 0 && othr < 3 && othr != prv && othr != cur);
                        int id = g[cur][d[cur] + 1];
                        op(dp[i + 1][x + (cur == 0)][y + (cur == 1)][cur], dp[i][x][y][prv] + max(pref[prv][id] - d[prv], 0) + max(pref[othr][id] - d[othr], 0));
                    }
                }

            }
        }
    }
    int ans = lim;
    for (int last = 0; last < 3; ++last) {
        ans = min(ans, dp[n][pref[0][n]][pref[1][n]][last]);
        // cout << pref[0][n] << ' ' << pref[1][n] << ' ' << last << ' ' <<dp[n][pref[0][n]][pref[1][n]][last] << nl;
    }
    // for (int x = 0; x <= n; ++x) {
    //     for (int y = 0; y <= n; ++y) {
    //         if (x + y > n) continue;
    //         for (int last = 0; last < 3; ++last) {
    //             ans = min(ans, dp[n][x][y][last]);
    //             if (dp[n][x][y][last] != lim) {
    //                 cout << x << ' ' << y << ' ' << last << ' ' << dp[n][x][y][last] << nl;
    //             }
    //         }
    //     }
    // }
    cout << (ans == lim ? -1 : ans) << nl;
}

signed main () {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int ttt = 1;
    if (T) cin >> ttt;
    while (ttt--) {
        ma1n();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 364 ms 774472 KB Output is correct
2 Correct 369 ms 774628 KB Output is correct
3 Correct 387 ms 774472 KB Output is correct
4 Correct 362 ms 774484 KB Output is correct
5 Correct 359 ms 774476 KB Output is correct
6 Correct 374 ms 774556 KB Output is correct
7 Correct 413 ms 774472 KB Output is correct
8 Correct 423 ms 774488 KB Output is correct
9 Correct 431 ms 774472 KB Output is correct
10 Correct 413 ms 774632 KB Output is correct
11 Correct 410 ms 774588 KB Output is correct
12 Correct 424 ms 774472 KB Output is correct
13 Correct 436 ms 774472 KB Output is correct
14 Correct 408 ms 774472 KB Output is correct
15 Correct 409 ms 774456 KB Output is correct
16 Correct 415 ms 774472 KB Output is correct
17 Correct 409 ms 774472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 364 ms 774472 KB Output is correct
2 Correct 369 ms 774628 KB Output is correct
3 Correct 387 ms 774472 KB Output is correct
4 Correct 362 ms 774484 KB Output is correct
5 Correct 359 ms 774476 KB Output is correct
6 Correct 374 ms 774556 KB Output is correct
7 Correct 413 ms 774472 KB Output is correct
8 Correct 423 ms 774488 KB Output is correct
9 Correct 431 ms 774472 KB Output is correct
10 Correct 413 ms 774632 KB Output is correct
11 Correct 410 ms 774588 KB Output is correct
12 Correct 424 ms 774472 KB Output is correct
13 Correct 436 ms 774472 KB Output is correct
14 Correct 408 ms 774472 KB Output is correct
15 Correct 409 ms 774456 KB Output is correct
16 Correct 415 ms 774472 KB Output is correct
17 Correct 409 ms 774472 KB Output is correct
18 Correct 429 ms 774472 KB Output is correct
19 Correct 426 ms 774524 KB Output is correct
20 Correct 412 ms 774528 KB Output is correct
21 Correct 414 ms 774556 KB Output is correct
22 Correct 389 ms 774476 KB Output is correct
23 Correct 412 ms 774544 KB Output is correct
24 Correct 437 ms 774540 KB Output is correct
25 Correct 436 ms 774688 KB Output is correct
26 Correct 397 ms 774592 KB Output is correct
27 Correct 374 ms 774472 KB Output is correct
28 Correct 378 ms 774664 KB Output is correct
29 Correct 367 ms 774568 KB Output is correct
30 Correct 385 ms 774656 KB Output is correct
31 Correct 377 ms 774484 KB Output is correct
32 Correct 379 ms 774664 KB Output is correct
33 Correct 440 ms 774464 KB Output is correct
34 Correct 429 ms 774564 KB Output is correct
35 Correct 442 ms 774492 KB Output is correct
36 Correct 438 ms 774436 KB Output is correct
37 Correct 410 ms 774472 KB Output is correct
38 Correct 406 ms 774624 KB Output is correct
39 Correct 424 ms 774468 KB Output is correct
40 Correct 424 ms 774596 KB Output is correct
41 Correct 421 ms 774472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 435 ms 774688 KB Output is correct
2 Correct 440 ms 774548 KB Output is correct
3 Correct 427 ms 774660 KB Output is correct
4 Correct 456 ms 774596 KB Output is correct
5 Correct 432 ms 774708 KB Output is correct
6 Correct 427 ms 774540 KB Output is correct
7 Correct 458 ms 774472 KB Output is correct
8 Correct 429 ms 774456 KB Output is correct
9 Correct 430 ms 774468 KB Output is correct
10 Correct 387 ms 774528 KB Output is correct
11 Correct 409 ms 774612 KB Output is correct
12 Correct 377 ms 774504 KB Output is correct
13 Correct 381 ms 774472 KB Output is correct
14 Correct 382 ms 774480 KB Output is correct
15 Correct 402 ms 774472 KB Output is correct
16 Correct 441 ms 774596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 364 ms 774472 KB Output is correct
2 Correct 369 ms 774628 KB Output is correct
3 Correct 387 ms 774472 KB Output is correct
4 Correct 362 ms 774484 KB Output is correct
5 Correct 359 ms 774476 KB Output is correct
6 Correct 374 ms 774556 KB Output is correct
7 Correct 413 ms 774472 KB Output is correct
8 Correct 423 ms 774488 KB Output is correct
9 Correct 431 ms 774472 KB Output is correct
10 Correct 413 ms 774632 KB Output is correct
11 Correct 410 ms 774588 KB Output is correct
12 Correct 424 ms 774472 KB Output is correct
13 Correct 436 ms 774472 KB Output is correct
14 Correct 408 ms 774472 KB Output is correct
15 Correct 409 ms 774456 KB Output is correct
16 Correct 415 ms 774472 KB Output is correct
17 Correct 409 ms 774472 KB Output is correct
18 Correct 429 ms 774472 KB Output is correct
19 Correct 426 ms 774524 KB Output is correct
20 Correct 412 ms 774528 KB Output is correct
21 Correct 414 ms 774556 KB Output is correct
22 Correct 389 ms 774476 KB Output is correct
23 Correct 412 ms 774544 KB Output is correct
24 Correct 437 ms 774540 KB Output is correct
25 Correct 436 ms 774688 KB Output is correct
26 Correct 397 ms 774592 KB Output is correct
27 Correct 374 ms 774472 KB Output is correct
28 Correct 378 ms 774664 KB Output is correct
29 Correct 367 ms 774568 KB Output is correct
30 Correct 385 ms 774656 KB Output is correct
31 Correct 377 ms 774484 KB Output is correct
32 Correct 379 ms 774664 KB Output is correct
33 Correct 440 ms 774464 KB Output is correct
34 Correct 429 ms 774564 KB Output is correct
35 Correct 442 ms 774492 KB Output is correct
36 Correct 438 ms 774436 KB Output is correct
37 Correct 410 ms 774472 KB Output is correct
38 Correct 406 ms 774624 KB Output is correct
39 Correct 424 ms 774468 KB Output is correct
40 Correct 424 ms 774596 KB Output is correct
41 Correct 421 ms 774472 KB Output is correct
42 Correct 435 ms 774688 KB Output is correct
43 Correct 440 ms 774548 KB Output is correct
44 Correct 427 ms 774660 KB Output is correct
45 Correct 456 ms 774596 KB Output is correct
46 Correct 432 ms 774708 KB Output is correct
47 Correct 427 ms 774540 KB Output is correct
48 Correct 458 ms 774472 KB Output is correct
49 Correct 429 ms 774456 KB Output is correct
50 Correct 430 ms 774468 KB Output is correct
51 Correct 387 ms 774528 KB Output is correct
52 Correct 409 ms 774612 KB Output is correct
53 Correct 377 ms 774504 KB Output is correct
54 Correct 381 ms 774472 KB Output is correct
55 Correct 382 ms 774480 KB Output is correct
56 Correct 402 ms 774472 KB Output is correct
57 Correct 441 ms 774596 KB Output is correct
58 Execution timed out 503 ms 774544 KB Time limit exceeded
59 Halted 0 ms 0 KB -