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<iostream>
#include<iomanip>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<string.h>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<cstdio>
#include<bitset>
#include<chrono>
#include<random>
#include<ext/rope>
/* ordered_set
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
*/
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 400 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e8;
int n;
string s;
int f[maxN][maxN][maxN][3];
int cntA[maxN][3], cntB[maxN][3], cntC[maxN][3];
void ReadInput()
{
cin >> n;
cin >> s;
s = ' ' + s;
}
void Solve()
{
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
for(int i=1; i<=n; i++)
{
if(s[i] == 'R')
{
cnt1++;
cntA[cnt1][1] = cnt2;
cntA[cnt1][2] = cnt3;
}
if(s[i] == 'G')
{
cnt2++;
cntB[cnt2][0] = cnt1;
cntB[cnt2][2] = cnt3;
}
if(s[i] == 'Y')
{
cnt3++;
cntC[cnt3][0] = cnt1;
cntC[cnt3][1] = cnt2;
}
}
memset(f, 37, sizeof f);
f[0][0][0][0] = f[0][0][0][1] = f[0][0][0][2] = 0;
for(int i=0; i<=cnt1; i++)
for(int j=0; j<=cnt2; j++)
for(int k=0; k<=cnt3; k++)
{
if(i < cnt1) f[i + 1][j][k][0] = min(f[i + 1][j][k][0], min(f[i][j][k][1], f[i][j][k][2]) + abs(cntA[i + 1][1] - j) + abs(cntA[i + 1][2] - k));
if(j < cnt2) f[i][j + 1][k][1] = min(f[i][j + 1][k][1], min(f[i][j][k][0], f[i][j][k][2]) + abs(cntB[j + 1][0] - i) + abs(cntB[j + 1][2] - k));
if(k < cnt3) f[i][j][k + 1][2] = min(f[i][j][k + 1][2], min(f[i][j][k][0], f[i][j][k][1]) + abs(cntC[k + 1][0] - i) + abs(cntC[k + 1][1] - j));
}
// cout << f[2][1][0][0];return;
int res = min({f[cnt1][cnt2][cnt3][0], f[cnt1][cnt2][cnt3][1], f[cnt1][cnt2][cnt3][2]}) / 2;
cout << (res < oo ? res : -1);
}
int32_t main()
{
//freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
# | 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... |