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;
const int maxn = 410;
int n;
int ans = 1e9;
string s;
bool check(string t)
{
for(int i = 0; i < t.size() - 1; i++)
if(t[i] == t[i + 1]) return false;
return true;
}
void next_try(string &t)
{
for(int p = t.size() - 1; p > 0; p--)
{
swap(t[p], t[p - 1]);
}
}
void generate(int a, int b, int c, string crct)
{
if((int)crct.size() == n)
{
vector<int> v1r, v1g, v1y;
vector<int> v2r, v2g, v2y;
for(int i = 0; i < n; i++)
{
if(s[i] == 'R')
{
v1r.push_back(i);
}
if(s[i] == 'G')
{
v1g.push_back(i);
}
if(s[i] == 'Y')
{
v1y.push_back(i);
}
if(crct[i] == 'R')
{
v2r.push_back(i);
}
if(crct[i] == 'G')
{
v2g.push_back(i);
}
if(crct[i] == 'Y')
{
v2y.push_back(i);
}
}
int num_dif = 0;
for(int i = 0; i < v1y.size(); i++)
{
num_dif += abs(v1y[i] - v2y[i]);
}
for(int i = 0; i < v1r.size(); i++)
{
num_dif += abs(v1r[i] - v2r[i]);
}
for(int i = 0; i < v1g.size(); i++)
{
num_dif += abs(v1g[i] - v2g[i]);
}
ans = min(ans, num_dif/2);
return;
}
else
{
if((int)crct.size())
{
if(a)
{
char last = crct[crct.size() - 1];
if(last != 'R') generate(a - 1, b, c, crct + "R");
}
if(b)
{
char last = crct[crct.size() - 1];
if(last != 'G') generate(a, b - 1, c, crct + "G");
}
if(c)
{
char last = crct[crct.size() - 1];
if(last != 'Y') generate(a, b, c - 1, crct + "Y");
}
}
else
{
if(a) generate(a - 1, b, c, crct + "R");
if(b) generate(a, b - 1, c, crct + "G");
if(c) generate(a, b, c - 1, crct + "Y");
}
}
}
string vazia;
int qt[4];
int main()
{
cin >> n;
for(int i = 0; i < n; i++)
{
char c;
cin >> c;
int tipo;
if(c == 'R') tipo = 1;
else if(c == 'G') tipo = 2;
else tipo = 3;
qt[tipo]++;
s += c;
}
string crct;
int metade = n/2 + (n % 2 == 1);
if(qt[1] > metade or qt[2] > metade or qt[3] > metade)
{
cout << -1 << "\n";
return 0;
}
int cont = 0;
//cout << crct << "\n";
generate(qt[1], qt[2], qt[3], vazia);
cout << (ans == (int)1e9 ? -1 : ans) << "\n";
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'bool check(std::__cxx11::string)':
joi2019_ho_t3.cpp:11:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < t.size() - 1; i++)
~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'void generate(int, int, int, std::__cxx11::string)':
joi2019_ho_t3.cpp:63:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v1y.size(); i++)
~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:67:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v1r.size(); i++)
~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:71:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v1g.size(); i++)
~~^~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:143:6: warning: unused variable 'cont' [-Wunused-variable]
int cont = 0;
^~~~
# | 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... |