This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma optimize("Bismillahirrahmanirrahim")
//█▀█─█──█──█▀█─█─█
//█▄█─█──█──█▄█─█■█
//█─█─█▄─█▄─█─█─█─█
//Allahuekber
//ahmet23 orz...
//Sani buyuk Osman Pasa Plevneden cikmam diyor.
//FatihSultanMehmedHan
//YavuzSultanSelimHan
//AbdulhamidHan
#define author tolbi
#include <bits/stdc++.h>
using namespace std;
template<typename X, typename Y> istream& operator>>(istream& in, pair<X,Y> &pr) {return in>>pr.first>>pr.second;}
template<typename X, typename Y> ostream& operator<<(ostream& os, pair<X,Y> pr) {return os<<pr.first<<" "<<pr.second;}
template<typename X> istream& operator>>(istream& in, vector<X> &arr) {for(auto &it : arr) in>>it; return in;}
template<typename X> ostream& operator<<(ostream& os, vector<X> arr) {for(auto &it : arr) os<<it<<" "; return os;}
template<typename X, size_t Y> istream& operator>>(istream& in, array<X,Y> &arr) {for(auto &it : arr) in>>it; return in;}
template<typename X, size_t Y> ostream& operator<<(ostream& os, array<X,Y> arr) {for(auto &it : arr) os<<it<<" "; return os;}
#define int long long
#define endl '\n'
#define vint(x) vector<int> x
#define deci(x) int x;cin>>x;
#define decstr(x) string x;cin>>x;
#define cinarr(x) for (auto &it : x) cin>>it;
#define coutarr(x) for (auto &it : x) cout<<it<<" ";cout<<endl;
#define sortarr(x) sort(x.begin(),x.end())
#define sortrarr(x) sort(x.rbegin(),x.rend())
#define det(x) cout<<"NO\0YES"+x*3<<endl;
#define INF LONG_LONG_MAX
#define rev(x) reverse(x.begin(),x.end());
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define tol(bi) (1LL<<((int)(bi)))
const int MOD = 1e9+7;
mt19937 ayahya(chrono::high_resolution_clock().now().time_since_epoch().count());
int old[401][401][3];
int dp[401][401][3];
int32_t main(){
ios;
int t=1;
int tno = 0;
if (!t) cin>>t;
while (t-(tno++)){
deci(n);
decstr(str);
vector<int> r;
vector<int> g;
vector<int> y;
for (int i = 0; i < n; ++i)
{
if (str[i]=='R') r.push_back(i);
else if (str[i]=='G') g.push_back(i);
else y.push_back(i);
}
for (int i = 0; i <= n; ++i)
{
for (int j = 0; j <= n; ++j){
for (int z = 0; z < 3; ++z)
{
old[i][j][z]=-1;
}
}
}
old[r.size()][g.size()][0]=0;
old[r.size()][g.size()][1]=0;
old[r.size()][g.size()][2]=0;
for (int i = n-1; i >= 0; i--){
for (int j = 0; j < n; j++){
for (int k = 0; k < n; k++){
dp[j][k][0]=dp[j][k][1]=dp[j][k][2]=MOD;
if (j>r.size() || k>g.size() || (i-j-k)>y.size()) continue;
if (j>i || k>i || (i-j-k)>i) continue;
if (j+k+y.size()<i) continue;
if (j<r.size()){
int lb = 0;
lb+=max(0ll,lower_bound(g.begin(), g.end(), r[j])-g.begin()-k);
lb+=max(0ll,lower_bound(y.begin(), y.end(), r[j])-y.begin()-(i-j-k));
if (old[j+1][k][1]!=-1) dp[j][k][0]=min(dp[j][k][0],old[j+1][k][1]+lb);
if (old[j+1][k][2]!=-1) dp[j][k][0]=min(dp[j][k][0],old[j+1][k][2]+lb);
}
if (k<g.size()){
int lb = 0;
lb+=max(0ll,lower_bound(r.begin(), r.end(), g[k])-r.begin()-j);
lb+=max(0ll,lower_bound(y.begin(), y.end(), g[k])-y.begin()-(i-j-k));
if (old[j][k+1][0]!=-1) dp[j][k][1]=min(dp[j][k][1],old[j][k+1][0]+lb);
if (old[j][k+1][2]!=-1) dp[j][k][1]=min(dp[j][k][1],old[j][k+1][2]+lb);
}
if (i-j-k<y.size()){
int lb = 0;
lb+=max(0ll,lower_bound(r.begin(), r.end(), y[i-j-k])-r.begin()-j);
lb+=max(0ll,lower_bound(g.begin(), g.end(), y[i-j-k])-g.begin()-k);
if (old[j][k][0]!=-1) dp[j][k][2]=min(dp[j][k][2],old[j][k][0]+lb);
if (old[j][k][1]!=-1) dp[j][k][2]=min(dp[j][k][2],old[j][k][1]+lb);
}
if (dp[j][k][0]==MOD) dp[j][k][0]=-1;
if (dp[j][k][1]==MOD) dp[j][k][1]=-1;
if (dp[j][k][2]==MOD) dp[j][k][2]=-1;
}
}
swap(old,dp);
}
int ans = -1;
for (int i = 0; i < 3; ++i)
{
if (old[0][0][i]!=-1){
if (ans==-1) ans=old[0][0][i];
ans=min(ans,old[0][0][i]);
}
}
cout<<ans<<endl;
}
}
Compilation message (stderr)
joi2019_ho_t3.cpp:1: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
1 | #pragma optimize("Bismillahirrahmanirrahim")
|
joi2019_ho_t3.cpp: In function 'int32_t main()':
joi2019_ho_t3.cpp:71:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | if (j>r.size() || k>g.size() || (i-j-k)>y.size()) continue;
| ~^~~~~~~~~
joi2019_ho_t3.cpp:71:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | if (j>r.size() || k>g.size() || (i-j-k)>y.size()) continue;
| ~^~~~~~~~~
joi2019_ho_t3.cpp:71:45: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | if (j>r.size() || k>g.size() || (i-j-k)>y.size()) continue;
| ~~~~~~~^~~~~~~~~
joi2019_ho_t3.cpp:73:22: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'long long int' [-Wsign-compare]
73 | if (j+k+y.size()<i) continue;
| ~~~~~~~~~~~~^~
joi2019_ho_t3.cpp:74:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | if (j<r.size()){
| ~^~~~~~~~~
joi2019_ho_t3.cpp:81:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | if (k<g.size()){
| ~^~~~~~~~~
joi2019_ho_t3.cpp:88:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | if (i-j-k<y.size()){
| ~~~~~^~~~~~~~~
# | 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... |