제출 #141039

#제출 시각아이디문제언어결과실행 시간메모리
141039meatrowGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
100 / 100
246 ms6912 KiB
//#pragma GCC optimize("O3") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native") //#pragma GCC optimize ("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int N = 405; int dp[N][N][4][2]; int cnt[3]; int n; int suf[N][N][3]; void init(int q) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int t = 0; t < 4; t++) { dp[i][j][t][q] = INT32_MAX; } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; map<char, int> mp; mp['R'] = 0; mp['G'] = 1; mp['Y'] = 2; string s; cin >> s; vector<int> pos[3]; for (int i = 0; i < n; i++) { cnt[mp[s[i]]]++; pos[mp[s[i]]].push_back(i); } for (int i = 0; i < 3; i++) { pos[i].push_back(-1); reverse(pos[i].begin(), pos[i].end()); } for (int t = 0; t < 3; t++) { for (int j = 0; j < n; j++) { for (int i = 1; i <= cnt[t]; i++) { suf[i][j][t] = suf[i - 1][j][t] + (pos[t][i] < j); } } } init(0); dp[cnt[0]][cnt[1]][3][0] = 0; for (int i = 0; i < n; i++) { int q = i & 1; int w = q ^ 1; init(w); for (int j = 0; j <= cnt[0]; j++) { for (int k = 0; k <= min(n - i - j, cnt[1]); k++) { for (int t = 0; t < 4; t++) { if (dp[j][k][t][q] == INT32_MAX) { continue; } if (j > 0 && t != 0) { dp[j - 1][k][0][w] = min(dp[j - 1][k][0][w], dp[j][k][t][q] + suf[k][pos[0][j]][1] + suf[n - i - j - k][pos[0][j]][2]); } if (k > 0 && t != 1) { dp[j][k - 1][1][w] = min(dp[j][k - 1][1][w], dp[j][k][t][q] + suf[j][pos[1][k]][0] + suf[n - i - j - k][pos[1][k]][2]); } if (n - i - j - k > 0 && t != 2) { dp[j][k][2][w] = min(dp[j][k][2][w], dp[j][k][t][q] + suf[j][pos[2][n - i - j - k]][0] + suf[k][pos[2][n - i - j - k]][1]); } } } } } int ans = INT32_MAX; for (int i = 0; i < 4; i++) { ans = min(ans, dp[0][0][i][n & 1]); } if (ans == INT32_MAX) { cout << -1; } else { cout << ans; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...