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;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
int convert(char c){
if (c == 'R') return 0;
if (c == 'G') return 1;
if (c == 'Y') return 2;
}
const int N = 500;
const int INF = 1e9 + 69;
vector<int> pos[3];
int pref[3][N];
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; cin >> n;
string s; cin >> s;
s = "#" + s;
for(int i = 1; i<=n; ++i){
int digit = convert(s[i]);
pos[digit].push_back(i);
pref[digit][i]++;
}
for(int j = 0; j < 3; ++j){
for(int i= 1; i<=n; ++i) pref[j][i] += pref[j][i-1];
}
int dp[pos[0].size() + 1][pos[1].size() + 1][pos[2].size() + 1][3];
for(int i = 0; i<=pos[0].size(); ++i)
for(int j = 0; j<=pos[1].size(); ++j)
for(int k = 0; k<=pos[2].size(); ++k)
for(int x = 0; x < 3; ++x) dp[i][j][k][x] = INF;
for(int x = 0; x<3; ++x) dp[0][0][0][x] = 0;
for(int i = 0; i<=pos[0].size(); ++i)
for(int j = 0; j<=pos[1].size(); ++j)
for(int k = 0; k<=pos[2].size(); ++k)
for(int x = 0; x < 3; ++x) if (dp[i][j][k][x] != INF){
if (x != 0 && i < pos[0].size()){
int cost1 = max(0, pref[1][pos[0][i]] - j);
int cost2 = max(0, pref[2][pos[0][i]] - k);
minimize(dp[i+1][j][k][0], dp[i][j][k][x] + cost1 + cost2);
}
if (x != 1 && j < pos[1].size()){
int cost1 = max(0, pref[0][pos[1][j]] - i);
int cost2 = max(0, pref[2][pos[1][j]] - k);
minimize(dp[i][j+1][k][1], dp[i][j][k][x] + cost1 + cost2);
}
if (x != 2 && k < pos[2].size()){
int cost1 = max(0, pref[0][pos[2][k]] - i);
int cost2 = max(0, pref[1][pos[2][k]] - j);
minimize(dp[i][j][k+1][2], dp[i][j][k][x] + cost1 + cost2);
}
}
int ans = INF;
for(int x = 0; x < 3; ++x) minimize(ans, dp[pos[0].size()][pos[1].size()][pos[2].size()][x]);
if (ans == INF) cout << -1 << "\n";
else cout << ans << "\n";
return 0;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:77:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i = 0; i<=pos[0].size(); ++i)
| ~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:78:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int j = 0; j<=pos[1].size(); ++j)
| ~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:79:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int k = 0; k<=pos[2].size(); ++k)
| ~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:84:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 0; i<=pos[0].size(); ++i)
| ~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:85:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int j = 0; j<=pos[1].size(); ++j)
| ~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:86:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int k = 0; k<=pos[2].size(); ++k)
| ~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:88:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | if (x != 0 && i < pos[0].size()){
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:93:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | if (x != 1 && j < pos[1].size()){
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:98:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | if (x != 2 && k < pos[2].size()){
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int convert(char)':
joi2019_ho_t3.cpp:51:1: warning: control reaches end of non-void function [-Wreturn-type]
51 | }
| ^
# | 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... |