Submission #704457

#TimeUsernameProblemLanguageResultExecution timeMemory
704457PenguinsAreCuteGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
0 / 100
54 ms276 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define SREP(i, a, b) for(ll i = a; i < b; i++) #define NREP(i, a, b) for(ll i = a; i <= b; i++) char f(char x) { if(x == 'R') return 'Y'; if (x == 'Y') return 'G'; else return 'R'; } int main2(ll N, string S) { ll R = 0, Y = 0, G = 0; //cin >> N >> S; vector<ll> Rpos, Ypos, Gpos; SREP(i, 0, N) { if(S[i] == 'R') { Rpos.push_back(i); R++; } else if(S[i] == 'Y') { Ypos.push_back(i); Y++; } else { Gpos.push_back(i); G++; } } ll dp[R + 1][Y + 1][G + 1][3]; NREP(i, 0, R) NREP(j, 0, Y) NREP(k, 0, G) SREP(l, 0, 3) dp[i][j][k][l] = 1e18; SREP(i, 0, 3) dp[0][0][0][i] = 0; NREP(i, 0, R) NREP(j, 0, Y) NREP(k, 0, G) { if(i != 0) dp[i][j][k][0] = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + abs(Rpos[i - 1] - (i + j + k - 1)); if(j != 0) dp[i][j][k][1] = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + abs(Ypos[j - 1] - (i + j + k - 1)); if(k != 0) dp[i][j][k][2] = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + abs(Gpos[k - 1] - (i + j + k - 1)); } ll ans = LONG_LONG_MAX; SREP(i, 0, 3) ans = min(ans, dp[R][Y][G][i]); if(ans < 1e12) return ans/2; else return -1; } int bruteforce(ll N, string S) { ll R = 0, Y = 0, G = 0; //cin >> N >> S; vector<ll> Rpos, Ypos, Gpos; SREP(i, 0, N) { if(S[i] == 'R') { Rpos.push_back(i); R++; } else if(S[i] == 'Y') { Ypos.push_back(i); Y++; } else { Gpos.push_back(i); G++; } } // try each string ll ans = LONG_LONG_MAX; SREP(j, 0, 3 * (ll)pow(2, N)) { ll ans2 = 0; string S2 = ""; ll k = j; if(k % 3 == 0) S2 += 'R'; else if(k % 3 == 1) S2 += 'Y'; else S2 += 'G'; k /= 3; SREP(i, 1, N) { if(k % 2 == 0) S2 += f(S2[i - 1]); else S2 += f(f(S2[i - 1])); k /= 2; } // try string s2 ll R2 = 0, Y2 = 0, G2 = 0; vector<ll> Rpos2, Ypos2, Gpos2; SREP(i, 0, N) { if(S2[i] == 'R') { Rpos2.push_back(i); R2++; } else if(S2[i] == 'Y') { Ypos2.push_back(i); Y2++; } else { Gpos2.push_back(i); G2++; } } if(R2 == R && Y2 == Y && G2 == G) { SREP(i, 0, R) ans2 += abs(Rpos[i] - Rpos2[i]); SREP(i, 0, Y) ans2 += abs(Ypos[i] - Ypos2[i]); SREP(i, 0, G) ans2 += abs(Gpos[i] - Gpos2[i]); ans = min(ans, ans2); } } if(ans == LONG_LONG_MAX) return -1; else return ans/2; } int main() { ll N; string S; cin >> N >> S; cout << bruteforce(N, S); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...