답안 #1087622

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1087622 2024-09-13T03:11:27 Z efishel Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
60 / 100
3 ms 1884 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;

vll ou[3];
bool vis[70][70][70][3];
ll dp[70][70][70][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

ll fposi (ll a, ll b, ll c, ll i) {
    ll 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;
ll 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 (vis[a][b][c][last]) return dp[a][b][c][last];
    vis[a][b][c][last] = true;
    ll &ans = dp[a][b][c][last];
    ans = 1e9;
    ll 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;
    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

joi2019_ho_t3.cpp:8:23: warning: multi-line comment [-Wcomment]
    8 | ll dp[70][70][70][3]; // dp[a][b][c][last] = min operations to have a valid string\
      |                       ^
joi2019_ho_t3.cpp: In function 'll solve(ll, ll, ll, ll)':
joi2019_ho_t3.cpp:27:24: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |     if (last != 0 && a < ou[0].size()) { ans = min(ans, solve(a+1, b, c, 0) + fposi(a, b, c, ou[0][a])-i); }
      |                      ~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:28:24: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     if (last != 1 && b < ou[1].size()) { ans = min(ans, solve(a, b+1, c, 1) + fposi(a, b, c, ou[1][b])-i); }
      |                      ~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:29:24: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     if (last != 2 && c < ou[2].size()) { ans = min(ans, solve(a, b, c+1, 2) + fposi(a, b, c, ou[2][c])-i); }
      |                      ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 464 KB Output is correct
16 Correct 0 ms 464 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 464 KB Output is correct
16 Correct 0 ms 464 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 2 ms 1372 KB Output is correct
21 Correct 3 ms 1624 KB Output is correct
22 Correct 1 ms 1112 KB Output is correct
23 Correct 2 ms 1348 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 0 ms 604 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 1372 KB Output is correct
28 Correct 2 ms 1372 KB Output is correct
29 Correct 2 ms 1372 KB Output is correct
30 Correct 2 ms 1232 KB Output is correct
31 Correct 2 ms 1116 KB Output is correct
32 Correct 2 ms 1372 KB Output is correct
33 Correct 1 ms 860 KB Output is correct
34 Correct 1 ms 860 KB Output is correct
35 Correct 1 ms 1372 KB Output is correct
36 Correct 2 ms 1116 KB Output is correct
37 Correct 1 ms 1116 KB Output is correct
38 Correct 1 ms 1116 KB Output is correct
39 Correct 2 ms 1216 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Runtime error 2 ms 1884 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 464 KB Output is correct
16 Correct 0 ms 464 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 2 ms 1372 KB Output is correct
21 Correct 3 ms 1624 KB Output is correct
22 Correct 1 ms 1112 KB Output is correct
23 Correct 2 ms 1348 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 0 ms 604 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 1372 KB Output is correct
28 Correct 2 ms 1372 KB Output is correct
29 Correct 2 ms 1372 KB Output is correct
30 Correct 2 ms 1232 KB Output is correct
31 Correct 2 ms 1116 KB Output is correct
32 Correct 2 ms 1372 KB Output is correct
33 Correct 1 ms 860 KB Output is correct
34 Correct 1 ms 860 KB Output is correct
35 Correct 1 ms 1372 KB Output is correct
36 Correct 2 ms 1116 KB Output is correct
37 Correct 1 ms 1116 KB Output is correct
38 Correct 1 ms 1116 KB Output is correct
39 Correct 2 ms 1216 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 604 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Runtime error 2 ms 1884 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -