제출 #1287073

#제출 시각아이디문제언어결과실행 시간메모리
1287073thirdGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++20
100 / 100
186 ms381848 KiB
#include<bits/stdc++.h> typedef int ll; #define pii pair<ll, ll> #define fi first #define se second #define endl '\n' #define TASK "" #define N 405 #define LOG 17 using namespace std; const ll inf = 1e9; bool ghuy4g; ll n, pf[N][3], idx[N][3], sum[3]; string s; map<char, ll> mp; void pre() { mp['R'] = 0; mp['G'] = 1; mp['Y'] = 2; for (int i = 1; i <= n; i ++) { for (int z = 0; z < 3; z ++) { pf[i][z] = pf[i - 1][z]; } pf[i][mp[s[i]]] ++ ; sum[mp[s[i]]] ++ ; idx[sum[mp[s[i]]]][mp[s[i]]] = i; // idx[i][z]: cai mau i thu z nam o vi tri nao } } int dp[402][402][402][3]; void solve() { for (int i = 0; i <= n; i ++) { for (int j = 0; j <= i; j ++) { for (int z = 0; z <= i; z ++) { for (int t = 0; t < 3; t ++) { dp[i][j][z][t] = inf; } } } } dp[0][0][0][0] = 0; dp[0][0][0][1] = 0; dp[0][0][0][2] = 0; for (int i = 1; i <= n; i ++) { for (int j = 0; j <= min(i, sum[0]); j ++) { // 0 for (int z = 0; z <= min(i - j, sum[1]); z ++) { // 1 dp[i][j][z][0] = inf; dp[i][j][z][1] = inf; dp[i][j][z][2] = inf; ll u = i - j - z; // 2 if (u > sum[2]) continue; // dien 0 if (j > 0) { // 0 ll pos = idx[j][0]; // vi tri cua cai thu j mau 0 ll them = 0; if (pf[pos][1] < z) { them += z - pf[pos][1]; } if (pf[pos][2] < u) { them += u - pf[pos][2]; } ll cost = abs(i - (pos + them)); dp[i][j][z][0] = min(dp[i][j][z][0], dp[i - 1][j - 1][z][1] + cost); dp[i][j][z][0] = min(dp[i][j][z][0], dp[i - 1][j - 1][z][2] + cost); } if (z > 0) { // 1 ll pos = idx[z][1]; ll them = 0; if (pf[pos][0] < j) { them += j - pf[pos][0]; } if (pf[pos][2] < u) { them += u - pf[pos][2]; } ll cost = abs(i - (pos + them)); dp[i][j][z][1] = min(dp[i][j][z][1], dp[i - 1][j][z - 1][0] + cost); dp[i][j][z][1] = min(dp[i][j][z][1], dp[i - 1][j][z - 1][2] + cost); } if (u > 0) { // 2 ll pos = idx[u][2]; ll them = 0; if (pf[pos][0] < j) { them += j - pf[pos][0]; } if (pf[pos][1] < z) { them += z - pf[pos][1]; } ll cost = abs(i - (pos + them)); dp[i][j][z][2] = min(dp[i][j][z][2], dp[i - 1][j][z][0] + cost); dp[i][j][z][2] = min(dp[i][j][z][2], dp[i - 1][j][z][1] + cost); } } } } ll ans = min({dp[n][sum[0]][sum[1]][0], dp[n][sum[0]][sum[1]][1], dp[n][sum[0]][sum[1]][2]}); if (ans >= inf) ans = -1; cout << ans; } bool klinh; signed main() { // freopen("test.inp", "r", stdin); //freopen("test.out", "w", stdout); //srand(time(0)); ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; cin >> s; s = '*' + s; pre(); solve(); cerr << fabs(&klinh - &ghuy4g) / double(1024 * 1024); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...