Submission #1118852

#TimeUsernameProblemLanguageResultExecution timeMemory
1118852gdragonGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
15 / 100
2 ms504 KiB
#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 400 + 5;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
int n;
int a[N];
string s;
void read() {
    cin >> n >> s;
    for(int i = 0; i < n; i++) {
        if (s[i] == 'R') a[i + 1] = 0;
        else if (s[i] == 'G') a[i + 1] = 1;
        else a[i + 1] = 2;
    }
}
bool check3() {
    for(int i = 1; i <= n; i++) if (a[i] == 2) return false;
    return true;
}
void sub3() {
    vector<int> cnt[2];
    for(int i = 1; i <= n; i++) cnt[a[i]].push_back(i);
    if (abs(SZ(cnt[0]) - SZ(cnt[1])) > 1) {
        cout << -1;
        return;
    }
    if (n & 1) {
        if (SZ(cnt[0]) < SZ(cnt[1])) swap(cnt[0], cnt[1]);
        reverse(ALL(cnt[0]));
        reverse(ALL(cnt[1]));
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            int c = 1;
            if (i & 1) c = 0;
            int p = cnt[c].back(); cnt[c].pop_back();
            for(int j = 0; j < 2; j++) {
                for(auto &pos: cnt[j]) if (pos < p) ++pos;
            }
            ans += p - i;
            // cerr << p << ' ' << i << ' ' << ans << endl;
        }
        cout << ans;
    }
    else {
        vector<int> tmp[2];
        tmp[0] = cnt[0];
        tmp[1] = cnt[1];
        int ans = inf;
        for(int iter = 0; iter < 2; iter++) {
            cnt[0] = tmp[0];
            cnt[1] = tmp[1];
            reverse(ALL(cnt[0]));
            reverse(ALL(cnt[1]));
            int res = 0;
            for(int i = 1; i <= n; i++) {
                int c = 1;
                if (i & 1) c = 0;
                int p = cnt[c].back(); cnt[c].pop_back();
                for(int j = 0; j < 2; j++) {
                    for(auto &pos: cnt[j]) if (pos < p) ++pos;
                }
                res += p - i;
            }
            ans = min(ans, res);
            swap(tmp[0], tmp[1]);
        }
        cout << ans;
    }
}
void solve() {
    if (check3()) sub3();
}
signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    int test = 1;
    // cin >> test;
    while(test--) {
        read();
        solve();
    }
    return 0;
}

// rmq - rmq2d
// hash
// fw - fw2d
// segtree

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...