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;
int n, cont = 0;
string s;
inline int func(char x, char y)
{
queue < int > fila;
if(s[0] != x) fila.push(0);
s[0] = x;
for(int i = 1 ; i < s.size() ; i++)
{
//cout << i << ' ' << s[i] << endl;
if(s[i] == x and fila.size())
{
//cout << i << ' ' << x << ' ' << fila.size() << ' ' << fila.front() << endl;
cont += i - fila.front();
fila.pop();
s[i] = y;
}
if(s[i] == s[i-1])
{
if(x == s[i])
{
s[i] = y;
fila.push(i);
char aux = x;
x = y;
y = aux;
}
else
{
fila.push(i);
s[i] = x;
}
}
}
if(fila.size()) return -1;
return cont;
}
int main()
{
cin >> n;
cin >> s;
string s2 = s;
int r = 0, g = 0;
for(int i = 0 ; i < s.size() ; i++)
{
if(s[i] == 'R') r++;
else g++;
}
int c1, c2;
if(r > g+1 or g > r+1) cout << -1 << endl;
else
{
if(r > g) c1 = func('R', 'G');
else if(g > r) c1 = func('G', 'R');
else
{
c1 = func(s[0], s[0] == 'G' ? 'R' : 'G');
c1 = min(c1, func(s[0] == 'G' ? 'R' : 'G', s[0]));
}
reverse(s2.begin(), s2.end());
s = s2;
cont = 0;
//cout << "OU\n";
if(r > g) c2 = func('R', 'G');
else if(g > r) c2 = func('G', 'R');
else
{
c2 = func(s[0], s[0] == 'G' ? 'R' : 'G');
c2 = min(c2, func(s[0] == 'G' ? 'R' : 'G', s[0]));
}
//cout << c1 << ' ' << c2 << endl;
cout << min(c1, c2) << endl;
}
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int func(char, char)':
joi2019_ho_t3.cpp:12:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1 ; i < s.size() ; i++)
~~^~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:49:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < s.size() ; i++)
~~^~~~~~~~~~
# | 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... |