Submission #1161343

#TimeUsernameProblemLanguageResultExecution timeMemory
1161343tsengangGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++20
5 / 100
1095 ms328 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define ertunt return
const int mod = 998244353;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin >> n;
    string s, t;
    cin >> s;
    t = s;
    sort(all(s));
    ll ans = 1e18;
    do {
        bool k = false;
        for (ll i = 1; i < n; i++) {
            if (s[i] == s[i - 1]) {
                k = true;
                break;
            }
        }
        if (k) continue;
        ll res = 0;
        string u = s;
        for (ll i = 0; i < n; i++) {
            for (ll j = i; j < n; j++) {
                if (t[i] == u[j]) {
                    for (ll k = j; k > i; k--) {
                        swap(u[k], u[k - 1]);
                        res++;
                    }
                    break;
                }
            }
        }
        ans = min(ans, res);
    } while (next_permutation(all(s)));
    cout << (ans == 1e18 ? -1 : ans);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...