제출 #119988

#제출 시각아이디문제언어결과실행 시간메모리
119988tutisGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
100 / 100
297 ms29256 KiB
/*input 20 YYGYYYGGGGRGYYGRGRYG */ #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; int code(char c) { if (c == 'R') return 0; if (c == 'G') return 1; return 2; } int main() { ios_base::sync_with_stdio(false); int n; string s; cin >> n >> s; vector<int>p[3]; for (int i = 0; i < n; i++) p[code(s[i])].push_back(i); int dp[p[0].size() + 2][p[1].size() + 2][p[2].size() + 2][3]; for (int k0 = 0; k0 <= (int)p[0].size(); k0++) { for (int k1 = 0; k1 <= (int)p[1].size(); k1++) { for (int k2 = 0; k2 <= (int)p[2].size(); k2++) { for (int a = 0; a < 3; a++) dp[k0][k1][k2][a] = 2e8; } } } dp[0][0][0][0] = 0; dp[0][0][0][1] = 0; dp[0][0][0][2] = 0; int k[3]; for (k[0] = 0; k[0] <= (int)p[0].size(); k[0]++) { for (k[1] = 0; k[1] <= (int)p[1].size(); k[1]++) { for (k[2] = 0; k[2] <= (int)p[2].size(); k[2]++) { for (int a = 0; a < 3; a++) { int cost = 0; if (k[a] == (int)p[a].size()) continue; for (int b = 0; b < 3; b++) if (b != a) { cost += lower_bound(p[b].begin() + k[b], p[b].end(), p[a][k[a]]) - (p[b].begin() + k[b]); } for (int b = 0; b < 3; b++) { if (a == b) continue; dp[k[0] + (a == 0)][k[1] + (a == 1)][k[2] + (a == 2)][b] = min(dp[k[0] + (a == 0)][k[1] + (a == 1)][k[2] + (a == 2)][b], cost + dp[k[0]][k[1]][k[2]][a]); } } } } } int ans = min({dp[p[0].size()][p[1].size()][p[2].size()][0], dp[p[0].size()][p[1].size()][p[2].size()][1], dp[p[0].size()][p[1].size()][p[2].size()][2] }); if (ans > 1e8) ans = -1; cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...