#include <bits/stdc++.h>
#define int long long
using namespace std;
int dp[3][70][70][70][70]; // 0 = r, 1 = g, 2 = y
int n;
vector<int> r, g, y;
string s;
int nxt[3][70][70][70][70];
int rec(int pos, int rp, int gp, int yp, int lst) {
if (pos == n) return 0;
if (dp[lst][pos][rp][gp][yp] != -1) return dp[lst][pos][rp][gp][yp];
int res = 1e15;
if (!r.empty() && r.size()!=rp && lst!=0) {
int val = rec(pos+1, rp+1, gp, yp, 0) + abs(r[rp]-pos);
if (val < res) {
res = val;
nxt[lst][pos][rp][gp][yp] = 0;
}
}
if (!g.empty() && g.size()!=gp && lst!=1) {
int val = rec(pos+1, rp, gp+1, yp, 1) + abs(g[gp]-pos);
if (val < res) {
res = val;
nxt[lst][pos][rp][gp][yp] = 1;
}
}
if (!y.empty() && y.size()!=yp && lst!=2) {
int val = rec(pos+1, rp, gp, yp+1, 2) + abs(y[yp]-pos);
if (val < res) {
res = val;
nxt[lst][pos][rp][gp][yp] = 2;
}
}
dp[lst][pos][rp][gp][yp] = res;
return res;
}
int fast() {
r = {}, g = {}, y = {};
for (int i = 0; i < 3; i++)
for (int a = 0; a <= n; a++)
for (int b = 0; b <= n; b++)
for (int c = 0; c <= n; c++)
for (int d = 0; d <= n; d++)
dp[i][a][b][c][d] = -1;
for (int i = 0; i < n; i++) {
if (s[i] == 'R') r.push_back(i);
if (s[i] == 'G') g.push_back(i);
if (s[i] == 'Y') y.push_back(i);
}
int res = min(rec(0,0,0,0,1),rec(0,0,0,0,0));
if (res >= 1e15) return -1;
else return res/2;
}
int slow() {
queue<pair<string,int>> q;
q.push({s,0});
set<string> vis;
vis.insert(s);
while (!q.empty()) {
pair<string,int> p = q.front();
q.pop();
bool ok = true;
for (int i = 1; i < s.length(); i++) {
if (p.first[i] == p.first[i-1]) {
ok = false;
}
}
if (ok) {
cout << "End state: " << p.first << " dist = " << p.second << '\n';
return p.second;
}
for (int i = 1; i < p.first.length(); i++) {
string t = p.first;
swap(t[i],t[i-1]);
if (vis.count(t) == 0) {
vis.insert(t);
q.push({t,p.second+1});
}
}
}
return -1;
}
char a[8];
void gen(int x) {
if (x == n) {
s = "";
for (int i = 0; i < n; i++) s += a[i];
int fa = fast();
int sl = slow();
if (fa != sl) {
cout << "WA, fast=" << fa << " slow=" << sl << '\n';
cout << s << '\n';
exit(0);
}
return;
}
a[x] = 'R';
gen(x+1);
a[x] = 'G';
gen(x+1);
a[x] = 'Y';
gen(x+1);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
//n = 9;
//gen(0);
cin >> n >> s;
fast();
int pos = 0, rp = 0, gp = 0, yp = 0, lst = 1;
string str = "";
while (pos != n) {
int to = nxt[lst][pos][rp][gp][yp];
if (to == 0) {
str += 'R';
rp++;
}
if (to == 1) {
str += 'G';
gp++;
}
if (to == 2) {
str += 'Y';
yp++;
}
lst = to;
pos++;
}
//cout << "dp end state = " << str << '\n';
int swaps = 0;
for (int i = 0; i < str.length(); i++) {
if (s[i] != str[i]) {
int p = -1;
for (int j = i+1; j < str.length(); j++) {
if (s[j] == str[i]) {
p = j;
break;
}
}
for (int j = p; j >= i+1; j--) {
swap(s[j],s[j-1]);
swaps++;
}
}
}
cout << swaps << '\n';
}
Compilation message
joi2019_ho_t3.cpp: In function 'long long int rec(long long int, long long int, long long int, long long int, long long int)':
joi2019_ho_t3.cpp:17:31: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
17 | if (!r.empty() && r.size()!=rp && lst!=0) {
| ~~~~~~~~^~~~
joi2019_ho_t3.cpp:24:31: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
24 | if (!g.empty() && g.size()!=gp && lst!=1) {
| ~~~~~~~~^~~~
joi2019_ho_t3.cpp:31:31: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
31 | if (!y.empty() && y.size()!=yp && lst!=2) {
| ~~~~~~~~^~~~
joi2019_ho_t3.cpp: In function 'long long int slow()':
joi2019_ho_t3.cpp:69:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i = 1; i < s.length(); i++) {
| ~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:78:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int i = 1; i < p.first.length(); i++) {
| ~~^~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:140:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
140 | for (int i = 0; i < str.length(); i++) {
| ~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:143:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
143 | for (int j = i+1; j < str.length(); j++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
4332 KB |
Output is correct |
5 |
Incorrect |
6 ms |
10732 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
4332 KB |
Output is correct |
5 |
Incorrect |
6 ms |
10732 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
56300 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
4332 KB |
Output is correct |
5 |
Incorrect |
6 ms |
10732 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |