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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long 
using namespace std;
int n,m,cnt,tot,c[27][200010];
ll ans;
char ss[200010];
struct node
{
	int l,r;
}q[100010];
struct seg
{
	int v[800010],lz[1000000];
	void pd(int i)
	{
		if(lz[i])
		{
			lz[i*2]+=lz[i];lz[i*2+1]+=lz[i];
			v[i*2]+=lz[i]; v[i*2+1]+=lz[i];
			lz[i]=0;
		}
	}
	void bu(int i,int l,int r)
	{
		if(l==r){v[i]=l;return ;}
		bu(i*2,l,(l+r)/2);bu(i*2+1,(l+r)/2+1,r);
	}
	void add(int i,int l,int r,int ql,int qr)
	{
		if(l>qr || ql>r)return ;
		if(ql<=l && r<=qr)
		{
			v[i]--;lz[i]--;
			return ;
		}
		pd(i);
		add(i*2,l,(l+r)/2,ql,qr);
		add(i*2+1,(l+r)/2+1,r,ql,qr);
	}	
	int qy(int i,int l,int r,int k)
	{
		if(l==r)return v[i];
		pd(i);
		if(k<=(l+r)/2)return qy(i*2,l,(l+r)/2,k);
		return qy(i*2+1,(l+r)/2+1,r,k);
	}
}tr;
bool cmp(node a,node b)
{
	return a.l<b.l;
}
int dic(int k)
{
	return k-'a'+1;
}
int main()
{
	int i,s,a,b,j,k;
	int st,ed;
	scanf(" %s",ss);n=strlen(ss);
	for(i=1;i<=n;i++)k=dic(ss[i-1]),c[k][++c[k][0]]=i;
	for(i=1;i<=26;i++)cnt+=(c[i][0]&1);
	if(cnt>1){printf("-1");return 0;}
	for(i=1;i<=26;i++)
		if(c[i][0]&1)
		{
			k=c[i][(c[i][0]+1)/2];
			for(s=1;s<=26;s++)
			{
				for(j=1;j<=c[s][0]/2;j++)
				{
					if(c[s][j]>k)ans++;
					if(c[s][c[s][0]-j+1]<k)ans++;
				}
			}
			n--;
			for(s=k-1;s<n;s++)ss[s]=ss[s+1];
			for(s=1;s<=26;s++)c[s][0]=0;
			for(s=1;s<=n;s++)k=dic(ss[s-1]),c[k][++c[k][0]]=s;
			break;
		}
	//cout<<"Ans "<<ans<<endl;
	for(i=1;i<=26;i++)
	{
		st=1;ed=c[i][0];
		while(st<ed)q[++tot].l=c[i][st++],q[tot].r=c[i][ed--];
	}
	if(n)
	{
		sort(q+1,q+1+tot,cmp);tr.bu(1,1,n);
		//for(i=1;i<=tot;i++)cout<<" "<<q[i].l<<" "<<q[i].r<<endl;
		for(i=1;i<=tot;i++)
		{
			//cout<<" "<<i<<" "<<q[i].l<<" "<<tr.qy(1,1,n,q[i].l)<<" "<<q[i].r<<" "<<tr.qy(1,1,n,q[i].r)<<endl;
			ans+=abs(n-i+1-tr.qy(1,1,n,q[i].r));
			tr.add(1,1,n,q[i].r,n);
			//cout<<" "<<ans<<endl;
		}
	}
	cout<<ans<<endl;
	return 0;
}