//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, b, a) for (int i = b; i >= a; --i)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define lsb(x) (x)&(-x)
#define bug(x) cerr << #x << " " << x << endl;
void setIO(string name = "") {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
ll fexp(ll a, ll b, ll m) {
if (b == 0) return 1LL;
ll p = a;
ll ans = 1;
while (b > 0) {
if (b % 2 != 0) ans = (ans * p) % m;
p = (p * p) % m;
b >>= 1;
}
return ans;
}
const int MAXN = 4e2 + 10;
const int INF = 1e18 + 5;
const int MOD = 1e9 + 7;
int dp[MAXN][MAXN][MAXN][3], cnt[MAXN][3], a[MAXN], p[MAXN][3];
void solve() {
int n;cin >> n;
rep(i,1,n) {
char ch;cin >> ch;
if (ch=='R') a[i] = 0;
else if (ch == 'G')a[i] = 1;
else a[i] = 2;
}
rep(i,1,n) rep(j,0,2) cnt[i][j] = cnt[i-1][j] + (a[i]==j);
int c[3] = {};
rep(i,1,n) p[++c[a[i]]][a[i]] = i;
rep(x,0,cnt[n][0]) {
rep(y,0,cnt[n][1]) {
rep(z,0,cnt[n][2]) {
rep(i,0,2) dp[x][y][z][i] = INF;
}
}
}
rep(i,0,2) dp[0][0][0][i] = 0;
rep(x,0,cnt[n][0]) {
rep(y,0,cnt[n][1]) {
rep(z,0,cnt[n][2]) {
vector<int> t = {x,y,z};
rep(i,0,2) {
if (t[i]==0) continue;
int j = (i+1)%3, k = (i+2)%3, pos = p[t[i]][i];
int inv = max(0LL,cnt[pos][j]-t[j]) + max(0LL,cnt[pos][k]-t[k]);
rep(l,0,2) {
if (i==l) continue;
dp[x][y][z][i] = min(dp[x][y][z][i], dp[x-(i==0)][y-(i==1)][z-(i==2)][l] + inv);
}
}
}
}
}
int ans = INF;
rep(i,0,2) ans = min(ans, dp[cnt[n][0]][cnt[n][1]][cnt[n][2]][i]);
cout << (ans==INF?-1:ans) << '\n';
}
int32_t main() {
setIO();
int tt = 1;
//cin >> tt;
while (tt-- > 0) solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
joi2019_ho_t3.cpp: In function 'void setIO(std::string)':
joi2019_ho_t3.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:30:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |