Problem:
Score : 800 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Determine whether we can turn S into a palindrome by repeating the operation of swapping two adjacent characters. If it is possible, find the minimum required number of operations.

Constraints

1 \leq |S| \leq 2 × 10^5

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If we cannot turn S into a palindrome, print -1. Otherwise, print the minimum required number of operations.

Sample Input 1

eel

Sample Output 1

1

We can turn S into a palindrome by the following operation:

Swap the 2-nd and 3-rd characters. S is now ele.

Sample Input 2

ataatmma

Sample Output 2

4

We can turn S into a palindrome by the following operation:

Swap the 5-th and 6-th characters. S is now ataamtma.

Swap the 4-th and 5-th characters. S is now atamatma.

Swap the 3-rd and 4-th characters. S is now atmaatma.

Swap the 2-nd and 3-rd characters. S is now amtaatma.

Sample Input 3

snuke

Sample Output 3

-1

We cannot turn S into a palindrome.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

using namespace std;

template <typename T=int>
struct Fenwick {
    int n;
    vector<T> v;
    
    Fenwick(int size = 100005) : n(size), v(n+1, 0) {}
    inline void add(int p, T val) {
        for (; p <= n; p += (p&-p)) { v[p] += val; }
    }
    inline T query(int p) {
        T tmp = 0;
        for (; p > 0; p -= (p&-p)) { tmp += v[p]; }
        return tmp;
    }
    inline T query(int l, int r) {
        return query(r) - query(l-1);
    }
};

void solve() {
    string s; cin >> s;
    int n = s.size();
    vector<vector<int>> pos(26);
    for (int i = 0; i < n; i++) {
        pos[s[i]-'a'].emplace_back(i);
    }
    vector<int> l(26, 0), r(26);
    vector<int> odd;
    for (int i = 0; i < 26; i++) {
        r[i] = pos[i].size();
        if (r[i]&1) odd.emplace_back(i);;
    }
    if (odd.size() > (n&1)) {
        cout << -1; return;
    }

    vector<bool> trk(n, false);
    vector<int> a(n);
    if (n&1) {
        int i = odd[0];
        int p = pos[i][r[i]/2];
        trk[p] = true;
        a[p] = n/2;
    }
    int L = 0, R = n;
    for (int i = 0; i < n; i++) {
        if (trk[i]) continue;
        int k = s[i]-'a';
        a[i] = L++;
        --r[k];
        a[pos[k][r[k]]] = --R;
        trk[i] = trk[pos[k][r[k]]] = true;
    }
    //for (int i = 0; i < n; i++) {
    //    cout << a[i] << '\n';
    //}
    reverse(a.begin(), a.end());
    long long res = 0;
    Fenwick<int> st(n);
    for (int x: a){
        x++;
        res += st.query(x);
        st.add(x, 1);
    }
    cout << res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    cout << endl;
}