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: Wrong Answer)

#include <bits/stdc++.h>
#define maxn 2000005
using namespace std;
string s;
struct X
{
    long long lg, rg, cs;
};
X a[maxn];
vector <int> bangbeo[maxn];
long long d = 0, p[maxn], dem[maxn], check[maxn],chu[maxn], trigger = 0, n;
bool lf(X x, X y)
{
    return x.lg < y.lg;
}
void build(int l, int h, int i)
{
    if (l == h)
    {
        p[i] = 1;
        return;
    }

    int mid = (l + h) / 2;
    build(l, mid, i * 2);
    build(mid + 1, h, i * 2 + 1);

    p[i] = p[i * 2] + p[i * 2 + 1];
}

void update(int l, int h, int i, int v)
{
    if (l > v || h < v) return;
    if (l == h)
    {
        p[i] = 0;
        return;
    }

    int mid = (l + h) / 2;
    update(l, mid, i * 2, v);
    update(mid + 1, h, i * 2 + 1, v);

    p[i] = p[i * 2] + p[i * 2 + 1];
}

int tim(int l, int h, int i, int u, int v)
{
    if (l > v || h < u) return 0;
    if (u <= l && h <= v)
    {
        return p[i];
    }

    int mid = (l + h) / 2;
    int g1 = tim(l, mid, i * 2, u, v);
    int g2 = tim(mid + 1, h, i * 2 + 1, u, v);

    p[i] = p[i * 2] + p[i * 2 + 1];
    return g1 + g2;
}
void prep()
{
    for (int i = 1; i <= n; i++)
    {
        bangbeo[(int)s[i] - 'a'].push_back(i);
        dem[(int)s[i] - 'a']++;
    }

    for (int i = 0; i <= 30; i++)
    {
        int j = 0, gj = bangbeo[i].size() - 1;
        if (gj < 0) continue;
        chu[++d] = i;
        int mid = (bangbeo[i].size() - 1) / 2;
        while (j <= mid)
        {
            a[d].lg = bangbeo[i][j];
            //cout << i << ' ' << d << ' ' << a[d].lg << '\n';
            a[d].rg = bangbeo[i][gj];
            a[d].cs = d;
            j++; gj--;
        }

        if (dem[i] % 2 == 1) trigger++;
    }

    sort (a + 1, a + d + 1, lf);

    //for (int i = 1; i <= d; i++) cout << a[i].lg << ' ' << a[i].rg << '\n';
}

void solve()
{
    build(1, n, 1);
    int cnt = 0;
    for (int i = 1; i <= d; i++)
    {
        if (dem[chu[a[i].cs]] % 2 == 1)
        {
            if (a[i].lg > n / 2 + 1) continue;
            int da = tim(1, n, 1, 1, a[i].lg);
            int da2 = tim(1, n, 1, 1, n / 2 + 1);
            cnt += da2 - da;
            //cout << chu[a[i].cs] << ' ' << i << ' ' << a[i].lg << ' ' << n / 2 + 1 << '\n';
            update(1, n, 1, a[i].lg);
            continue;
        }

        int da = tim(1, n, 1, 1, a[i].rg);
        cnt += p[1] - da;
        //cout << a[i].lg << ' ' << a[i].rg << ' ' << p[1] << '\n';
        update(1, n, 1, a[i].rg);
    }

    cout << cnt;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen ("test.inp", "r", stdin);
    //freopen ("test.out", "w", stdout);
    cin >> s;
    n = s.length();
    s = ' ' + s;
    prep();
    if (trigger >= 2)
    {
        cout << -1;
        return 0;
    }
    solve();
}