This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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++)
ll bSearch(vector<ll> &vll, ll target, ll h) {
ll l = -1;
while(h - l > 1) {
ll mid = (l + h) / 2;
if(vll[mid] > target) h = mid;
else l = mid;
}
return h;
}
int main() {
ll N, R = 0, Y = 0, G = 0; string S; 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[2][Y + 1][G + 1][3];
SREP(i, 0, 2) 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 + j + k == 0) continue;
dp[i % 2][j][k][0] = 1e18;
dp[i % 2][j][k][1] = 1e18;
dp[i % 2][j][k][2] = 1e18;
if(i != 0) dp[i % 2][j][k][0] = min(dp[(i - 1) % 2][j][k][1], dp[(i - 1) % 2][j][k][2]) + j - bSearch(Ypos, Rpos[i - 1], j) + k - bSearch(Gpos, Rpos[i - 1], k);
if(j != 0) dp[i % 2][j][k][1] = min(dp[i % 2][j - 1][k][0], dp[i % 2][j - 1][k][2]) + i - bSearch(Rpos, Ypos[j - 1], i) + k - bSearch(Gpos, Ypos[j - 1], k);
if(k != 0) dp[i % 2][j][k][2] = min(dp[i % 2][j][k - 1][0], dp[i % 2][j][k - 1][1]) + i - bSearch(Rpos, Gpos[k - 1], i) + j - bSearch(Ypos, Gpos[k - 1], j);
}
ll ans = LONG_LONG_MAX;
SREP(i, 0, 3) ans = min(ans, dp[R % 2][Y][G][i]);
if(ans >= 1e12) cout << -1;
else cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |