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>
using namespace std;

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

typedef long long LL;

vector<int> kouhan;

LL mergecount(int l,int r){
  LL count=0;
  if(r-l>1){
    int mid=(l+r)/2;
    count+=mergecount(l,mid);
    count+=mergecount(mid,r);
    vector<int> a(kouhan.begin(),kouhan.begin()+mid);
    vector<int> b(kouhan.begin()+mid,kouhan.end());
    for(int i=0,j=0,k=0;i<r-l;++i){
      if(k==mid-l) kouhan[l+i]=a[j++];
      else if(j==r-mid) kouhan[l+i]=b[k++];
      else if(a[j]<=b[k]) kouhan[l+i]=a[j++];
      else kouhan[l+i]=b[k++],count+=(r-l)/2-j;
    }
  }
  // for(int i=l;i<r;i++) cout << kouhan[i] << " ";
  // cout << endl;
  // cout << l << r << count << endl;
  return count;
}

int main(){
  string s;
  cin >> s;
  int n=s.length();
  vector<int> sn(n),kosu(26,0),count(26,0);
  for(int i=0;i<n;i++){
    sn[i]=s[i]-'a';
    kosu[sn[i]]++;
  }
  int kisukosu=0,kisumoji;
  for(int i=0;i<26;i++){
    if(kosu[i]%2) kisukosu++,kisumoji=i;
  }
  if(n%2){
    if(kisukosu!=1){
      cout << -1 << endl;
      return 0;
    }
  }else{
    if(kisukosu!=0){
      cout << -1 << endl;
      return 0;
    }
  }
  kouhan=vector<int>(n/2);
  vector<vector<int>> idx=vector<vector<int>>(26);
  for(int i=0;i<26;i++){
    if(kosu[i]/2>0) idx[i]=vector<int>(kosu[i]/2,-1);
  }
  int now=0;
  LL ans=0;
  for(int i=0;i<n;i++){
    if(count[sn[i]]<kosu[sn[i]]/2){
      idx[sn[i]][count[sn[i]]]=now;
      ans+=i-now;
      now++;
      count[sn[i]]++;
    }
  }
  bool midflag=false;
  for(int i=0;i<26;i++) count[i]=0;
  now=0;
  for(int i=n-1;i>=0;i--){
    if(count[sn[i]]<kosu[sn[i]]/2){
      kouhan[now]=idx[sn[i]][count[sn[i]]];
      now++;
      count[sn[i]]++;
      if(midflag) ans++;
    }else if(n%2&&!midflag&&sn[i]==kisumoji){
      midflag=true;
    }
  }
  // cout << ans << endl;
  // for(int i=0;i<n/2;i++){
  //   cout << kouhan[i]<< " ";
  // }
  // cout << endl;
  // ans+=mergecount(0,n/2);
  cout << ans << endl;
  return 0;
}