이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
//#define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 401, inf = 1e9;
int t,n,a[N], dp[N][N][N][3],pr[N][3], cnt[3];
bool fix[N][N][N][3];
string s;
vector <int> vec[3];
int get_cnt(int le, int ri, int ty) {
if (le > ri) return 0;
return pr[ri][ty] - pr[le - 1][ty];
}
int get(int i, int j, int k, int ty) {
//cout<<"get "<<i<<" "<<j<<" "<<k<<" "<<ty<<"\n";
if (ty == 0) {
return get_cnt(vec[1][j] + 1, vec[0][i], 1) + get_cnt(vec[2][k] + 1, vec[0][i], 2);
}
if (ty == 1) {
return get_cnt(vec[0][i] + 1, vec[1][j], 0) + get_cnt(vec[2][k] + 1, vec[1][j], 2);
}
if (ty == 2) {
return get_cnt(vec[0][i] + 1, vec[2][k], 0) + get_cnt(vec[1][j] + 1, vec[2][k], 1);
}
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>s;
s = "@" + s;
for (int i = 0; i < 3; i++) {
vec[i].pb(0);
}
for (int i = 1; i <= n; i++) {
if (s[i] == 'R') s[i] = 'a'; else s[i] = (s[i] == 'G' ? 'b' : 'c');
vec[s[i] - 'a'].pb(i); cnt[s[i] - 'a']++;
for (int j = 0; j < 3; j++) {
pr[i][j] += pr[i - 1][j];
}
pr[i][s[i] - 'a']++;
}
for (int i = 0; i <= cnt[0]; i++) {
for (int j = 0; j <= cnt[1]; j++) {
for (int k = 0; k <= cnt[2]; k++) {
for (int lst = 0; lst <= 2; lst++) {
dp[i][j][k][lst] = inf;
if (i + j + k == 0) {
dp[i][j][k][lst] = 0; fix[i][j][k][lst] = -1;
}
}
}
}
}
for (int i = 0; i <= cnt[0]; i++) {
for (int j = 0; j <= cnt[1]; j++) {
for (int k = 0; k <= cnt[2]; k++) {
for (int pr_lst = 0; pr_lst <= 2; pr_lst++) {
if (!fix[i][j][k][pr_lst]) continue;
if (i < cnt[0] && pr_lst != 0) {
dp[i + 1][j][k][0] = min(dp[i + 1][j][k][0], dp[i][j][k][pr_lst] + get(i + 1, j, k, 0));
fix[i + 1][j][k][0] = 1;
}
if (j < cnt[1] && pr_lst != 1) {
dp[i][j + 1][k][1] = min(dp[i][j + 1][k][1], dp[i][j][k][pr_lst] + get(i, j + 1, k, 1)); fix[i][j + 1][k][1] = 1;
}
if (k < cnt[2] && pr_lst != 2) {
dp[i][j][k + 1][2] = min(dp[i][j][k + 1][2], dp[i][j][k][pr_lst] + get(i, j, k + 1, 2)); fix[i][j][k + 1][2] = 1;
}
}
}
}
}
int mn = min(dp[cnt[0]][cnt[1]][cnt[2]][0], dp[cnt[0]][cnt[1]][cnt[2]][1]);
mn = min(mn, dp[cnt[0]][cnt[1]][cnt[2]][2]);
cout<<(mn == inf ? -1 : mn)<<"\n";
}
컴파일 시 표준 에러 (stderr) 메시지
joi2019_ho_t3.cpp:29:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
29 | main() {
| ^~~~
joi2019_ho_t3.cpp: In function 'int get(int, int, int, int)':
joi2019_ho_t3.cpp:28:1: warning: control reaches end of non-void function [-Wreturn-type]
28 | }
| ^
# | 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... |