Submission #644930

#TimeUsernameProblemLanguageResultExecution timeMemory
644930ghostwriterGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
100 / 100
61 ms3924 KiB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const int oo = 1e9 + 5;
#define getid(x) (x == 'R'? 0 : x == 'G'? 1 : 2)
const int N = 405;
int n, pos[3][N], c[3], d[N][N][3], d1[N][N][3], d2[N][3][N];
char s[N];
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n;
    FOR(i, 1, n) {
    	cin >> s[i];
    	++c[getid(s[i])];
    	pos[getid(s[i])][c[getid(s[i])]] = i;
    }
    FOR(i, 1, n)
    FOR(j, 0, 2) {
    	if (j == getid(s[i])) continue;
	    FOR(z, 1, c[j]) d2[i][j][z] = d2[i][j][z - 1] + (pos[j][z] > i? 1 : 0);
	}
    FSN(i, c[0] + 1) {
	    FSN(j, c[1] + 1)
	    FSN(z, c[2] + 1)
	    FSN(x, 3) {
	    	int &rs = d[j][z][x];
	    	rs = oo;
	    	if (i == c[0] && j == c[1] && z == c[2]) {
	    		rs = 0;
	    		continue;
	    	}
	    	if (x != 0 && i < c[0]) rs = min(rs, d1[j][z][0] + d2[pos[0][i + 1]][1][j] + d2[pos[0][i + 1]][2][z]);
	    	if (x != 1 && j < c[1]) rs = min(rs, d[j + 1][z][1] + d2[pos[1][j + 1]][0][i] + d2[pos[1][j + 1]][2][z]);
	    	if (x != 2 && z < c[2]) rs = min(rs, d[j][z + 1][2] + d2[pos[2][z + 1]][0][i] + d2[pos[2][z + 1]][1][j]);
	    }
	    FSN(j, c[1] + 1)
	    FSN(z, c[2] + 1)
	    FSN(x, 3)
	    	d1[j][z][x] = d[j][z][x];
	}
	int rs = min({d[0][0][0], d[0][0][1], d[0][0][2]}); 
	cout << (rs < oo? rs : -1);
    return 0;
}
/*
5
RRGYY
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joi2019_ho_t3.cpp:59:5: note: in expansion of macro 'FOR'
   59 |     FOR(i, 1, n) {
      |     ^~~
joi2019_ho_t3.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joi2019_ho_t3.cpp:64:5: note: in expansion of macro 'FOR'
   64 |     FOR(i, 1, n)
      |     ^~~
joi2019_ho_t3.cpp:30:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joi2019_ho_t3.cpp:65:5: note: in expansion of macro 'FOR'
   65 |     FOR(j, 0, 2) {
      |     ^~~
joi2019_ho_t3.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joi2019_ho_t3.cpp:67:6: note: in expansion of macro 'FOR'
   67 |      FOR(z, 1, c[j]) d2[i][j][z] = d2[i][j][z - 1] + (pos[j][z] > i? 1 : 0);
      |      ^~~
joi2019_ho_t3.cpp:33:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
      |                            ^
joi2019_ho_t3.cpp:69:5: note: in expansion of macro 'FSN'
   69 |     FSN(i, c[0] + 1) {
      |     ^~~
joi2019_ho_t3.cpp:33:28: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   33 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
      |                            ^
joi2019_ho_t3.cpp:70:6: note: in expansion of macro 'FSN'
   70 |      FSN(j, c[1] + 1)
      |      ^~~
joi2019_ho_t3.cpp:33:28: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   33 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
      |                            ^
joi2019_ho_t3.cpp:71:6: note: in expansion of macro 'FSN'
   71 |      FSN(z, c[2] + 1)
      |      ^~~
joi2019_ho_t3.cpp:33:28: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
   33 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
      |                            ^
joi2019_ho_t3.cpp:72:6: note: in expansion of macro 'FSN'
   72 |      FSN(x, 3) {
      |      ^~~
joi2019_ho_t3.cpp:33:28: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   33 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
      |                            ^
joi2019_ho_t3.cpp:83:6: note: in expansion of macro 'FSN'
   83 |      FSN(j, c[1] + 1)
      |      ^~~
joi2019_ho_t3.cpp:33:28: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   33 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
      |                            ^
joi2019_ho_t3.cpp:84:6: note: in expansion of macro 'FSN'
   84 |      FSN(z, c[2] + 1)
      |      ^~~
joi2019_ho_t3.cpp:33:28: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
   33 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
      |                            ^
joi2019_ho_t3.cpp:85:6: note: in expansion of macro 'FSN'
   85 |      FSN(x, 3)
      |      ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...