이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 410;
int n;
int ans = 1e9;
string s;
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((int)crct.size() == 0)
{
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);
int cont = 0;
generate(qt[1], qt[2], qt[3], vazia);
cout << (ans == (int)1e9 ? -1 : ans) << "\n";
}
컴파일 시 표준 에러 (stderr) 메시지
joi2019_ho_t3.cpp: In function 'void generate(int, int, int, std::__cxx11::string)':
joi2019_ho_t3.cpp:46:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v1y.size(); i++)
~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:50:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v1r.size(); i++)
~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:54: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:118:6: warning: unused variable 'metade' [-Wunused-variable]
int metade = n/2 + (n % 2 == 1);
^~~~~~
joi2019_ho_t3.cpp:120: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... |