이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
// #include <bits/extc++.h>
using namespace std;
#define debug(s) cout << #s << " = " << s << endl
#define all(v) (v).begin(), (v).end()
#define KeepUnique(v) (v).erase( unique(all(v)), v.end() )
#define MEMSET(a, val) memset(a, val, sizeof (a))
#define PB push_back
#define endl '\n'
typedef long long ll;
inline int myrand(int l, int r) {
int ret = rand(); ret <<= 15; ret ^= rand();
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
return ret;
}
template <typename F, typename S>
ostream& operator << (ostream &os, const pair<F, S> &p) {
return os << "(" << p.first << ", " << p.second << ")"; }
typedef pair<int, int> ii;
template<typename T> using min_pq =
std::priority_queue<T, vector<T>, greater<T> >;
//int dx[] = {-1, +0, +1, +0};
//int dy[] = {+0, +1, +0, -1};
const int maxn = 407;
const int inf = 1e8;
int where[3][maxn >> 1][maxn >> 1][maxn >> 1];
int a[maxn], cnt[5], n;
vector<int> g[5];
int dp[3][maxn >> 1][maxn >> 1][maxn >> 1];
int f(int l, int i, int j, int k) {
if(i < 0 or j < 0 or k < 0) return inf;
if(i + j + k == 0) return 0;
int &ret = dp[l][i][j][k];
if(ret != -1) return ret;
ret = inf;
for(int op = 0; op < 3; op++) if(l != op) {
ret = min(ret, i + j + k - where[op][i][j][k] + f(op, i-(op==0), j-(op==1), k-(op==2)));
}
return ret;
}
int32_t main () {
// freopen("in", "r", stdin);
char str[maxn];
scanf("%d", &n);
scanf("%s", str + 1);
for(int i = 1; i <= n; i++) {
if(str[i] == 'R') g[0].emplace_back(i);
else if(str[i] == 'G') g[1].emplace_back(i);
else g[2].emplace_back(i);
}
for(int i = 0; i < 3; i++) {
for(int j : g[i]) a[j] = i;
cnt[i] = g[i].size();
}
// for(int i = 1; i <= n; i++) cout << a[i]; cout << endl;
for(int i = 0; i < 3; i++) if(g[i].size() > (n - 1) / 2 + 1) {
puts("-1"); exit(0);
}
int c[5];
for(c[0] = 0; c[0] <= cnt[0]; c[0]++) {
for(c[1] = 0; c[1] <= cnt[1]; c[1]++) {
for(c[2] = 0; c[2] <= cnt[2]; c[2]++) {
vector<int> tmp;
for(int id = 0; id < 3; id++) {
for(int l = 0; l < c[id]; l++)
tmp.emplace_back(g[id][l]);
}
sort(tmp.begin(), tmp.end());
for(int id = 0; id < 3; id++) {
if(c[id] != 0) {
where[id][c[0]][c[1]][c[2]] = lower_bound(tmp.begin(), tmp.end(), g[id][c[id] - 1]) - tmp.begin() + 1;
// cout << "where[" << id << "][" << c[0] << "][" << c[1] << "][" << c[2] << "]" << " = " << where[id][c[0]][c[1]][c[2]] << endl;
}
}
}
}
}
for(int l = 0; l < 3; l++) {
for(int i = 0; i <= cnt[0]; i++) {
for(int j = 0; j <= cnt[1]; j++) {
for(int k = 0; k <= cnt[2]; k++) {
dp[l][i][j][k] = -1;
}
}
}
}
int ans = inf;
for(int i = 0; i < 3; i++) {
ans = min(ans, f(i, cnt[0], cnt[1], cnt[2]));
}
cout << ans << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
joi2019_ho_t3.cpp: In function 'int myrand(int, int)':
joi2019_ho_t3.cpp:18:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~
joi2019_ho_t3.cpp:18:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~~
joi2019_ho_t3.cpp: In function 'int32_t main()':
joi2019_ho_t3.cpp:69:44: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < 3; i++) if(g[i].size() > (n - 1) / 2 + 1) {
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:57:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", str + 1);
~~~~~^~~~~~~~~~~~~~~
# | 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... |