Submission #1107387

#TimeUsernameProblemLanguageResultExecution timeMemory
1107387vjudge1Growing Vegetable is Fun 3 (JOI19_ho_t3)C++17
0 / 100
1 ms336 KiB
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0), cin.tie(0)
#define int long long
#define st first
#define nd second
#define pb push_back
#define love our_kolhoz

using namespace std;
const int N = 1e5 + 10, inf = 1e18, mod = 1e9 + 7;
//koz qorqaq

int n, g, r, y;
string a;

void solve() {

    cin >> n >> a;

    for (int i = 0; i < n; i++) {
        if (a[i] == 'R') {
            r++;
        }
        else if (a[i] == 'G') {
            g++;
        }
        else {
            y++;
        }
    }

    int kek[3] = {r, g, y};
    sort(kek, kek + 3);
    if (kek[2] - 1 > kek[1] + kek[0]) {
        cout << -1;
        return;
    }

    int ans = 0;
    for (int i = n - 1; i > 0; i--) {
        if (a[i] == a[i - 1]) {
            for (int j = i; j >= 0; j--) {
                if (a[j] != a[i]) {
                    swap(a[j], a[i - 1]);
                    ans += (i - 1 - j);
                    break;
                }
            }
        }
    }

    for (int i = 0; i < n - 1; i++) {
        if (a[i] == a[i + 1]) {
            for (int j = i + 1; j < n; j++) {
                if (a[j] != a[i]) {
                    swap(a[j], a[i + 1]);
                    ans += (j - i - 1);
                    break;
                }
            }
        }
    }

    //cout << a << ' ';

    cout << ans;


}

signed main() {

    speed;

    //freopen("points.in ", "r", stdin);
    //freopen("points.out", "w", stdout);

    int _t = 1;
    //cin >> _t;
    while (_t--)
        solve();


    return 0;

}
// sau bol
// there's really no way of winning
// if in their eyes you'll always be a dumb blonde...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...