#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
gp_hash_table<int, int> table;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define MOO(i, a, b) for(int i=a; i<b; i++)
#define M00(i, a) for(int i=0; i<a; i++)
#define MOOd(i,a,b) for(int i = (b)-1; i >= a; i--)
#define M00d(i,a) for(int i = (a)-1; i>=0; i--)
#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << '\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
#define _ << " _ " <<
template<class T> bool ckmin(T& a, const T& b) {return b < a ? a = b, 1 : 0;}
template<class T> bool ckmax(T& a, const T& b) {return a < b ? a = b, 1 : 0;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
struct chash {
int operator()(pair<pi, pair<pi,int>> x) const {
return 31*x.f.f + 17*x.f.s + 3*x.s.f.f + 13*x.s.f.s + 7*x.s.s;
}
};
const int MAX_N = 404;
int n;
int arr[MAX_N];
int pref[MAX_N][3];
vi idxOf[3];
gp_hash_table<pair<pi, pair<pi,int>>, int, chash> dp;
int getOf(int idx, pair<pi,int> fucked) {
if(idx == 0) return fucked.f.f;
if(idx == 1) return fucked.f.s;
return fucked.s;
}
int getcost(int c, pair<pi,int> cnt) { // cost to get the next c
if(getOf(c, cnt) >= sz(idxOf[c])) return 1e8; // impossible
int idx = idxOf[c][getOf(c, cnt)];
int res = idx;
M00(k, 3) res -= min(getOf(k, cnt), pref[idx][k]);
return res;
}
int getdp(int idx, int c, pair<pi,int> cnt) {
pair<pi, pair<pi,int>> tmp = mp(mp(idx, c), cnt);
if(dp.count(tmp)) return dp[tmp];
int cost = getcost(c, cnt);
if(cost == 1e8) {
dp[tmp] = 1e8;
return dp[tmp];
}
if(idx == n-1) {
dp[tmp] = cost;
return dp[tmp];
}
if(c == 0) {
cnt.f.f++;
} else if(c == 1) {
cnt.f.s++;
} else {
cnt.s++;
}
int res = 1e8;
M00(c2, 3) if(c2 != c) {
ckmin(res, cost + getdp(idx+1, c2, cnt));
}
dp[tmp] = res;
return dp[tmp];
}
int main() { FAST
dp.reserve(4096);
dp.max_load_factor(0.25);
string s;
cin >> n >> s;
M00(i, n) {
if(s[i] == 'R') arr[i] = 0;
else if(s[i] == 'Y') arr[i] = 1;
else arr[i] = 2;
}
M00(c, 3) {
pref[0][c] = (arr[0] == c);
MOO(i, 1, n) pref[i][c] = pref[i-1][c] + (arr[i] == c);
}
M00(i, n) idxOf[arr[i]].pb(i);
int res = 1e8;
M00(i, 3) ckmin(res, getdp(0, i, mp(mp(0,0),0)));
if(res == 1e8) {
finish(-1);
}
finish(res);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int getdp(int, int, std::pair<std::pair<int, int>, int>)':
joi2019_ho_t3.cpp:63:11: error: 'class __gnu_pbds::gp_hash_table<std::pair<std::pair<int, int>, std::pair<std::pair<int, int>, int> >, int, chash>' has no member named 'count'
if(dp.count(tmp)) return dp[tmp];
^~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:89:8: error: 'class __gnu_pbds::gp_hash_table<std::pair<std::pair<int, int>, std::pair<std::pair<int, int>, int> >, int, chash>' has no member named 'reserve'; did you mean 'resize'?
dp.reserve(4096);
^~~~~~~
resize
joi2019_ho_t3.cpp:90:8: error: 'class __gnu_pbds::gp_hash_table<std::pair<std::pair<int, int>, std::pair<std::pair<int, int>, int> >, int, chash>' has no member named 'max_load_factor'; did you mean 'm_grow_factor'?
dp.max_load_factor(0.25);
^~~~~~~~~~~~~~~
m_grow_factor