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 Go (1.6) (judge verdict: Accepted)

package main

import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
  "sort"
)

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  buffer := make([]byte, 0)
  for true {
    line, isPrefix, err := reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func NextInt(A ...*int) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.Atoi(L[i])
  }
}
func NextIntVec(A *[]int) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]int, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.Atoi(l)
  }
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A []int) {
  S := make([]string, len(A))
  for i, a := range A {
    S[i] = strconv.Itoa(a)
  }
  Write(strings.Join(S, " "))
}
func Output() {
  _ = writer.Flush()
}

func MaxInt(A ...int) int {
  max := A[0]
  for _, a := range A {
    if max < a { max = a }
  }
  return max
}
func MinInt(A ...int) int {
  min := A[0]
  for _, a := range A {
    if a < min { min = a }
  }
  return min
}
func SumOdd(A ...int) int {
  sum := 0
  for _, a := range A {
    sum += a % 2
  }
  return sum
}
type BinaryIndexedTree []int
func (BIT BinaryIndexedTree) Get(idx int) int {
  b := 0
  for i := idx; 0 < i; i -= i & (-i) {
    b += BIT[i]
  }
  return b
}
func (BIT *BinaryIndexedTree) Update(idx, v int) {
  N := len((*BIT))
  d := v - BIT.Get(idx) + BIT.Get(idx - 1)
  for i := idx; i < N; i += i & (-i) {
    (*BIT)[i] += d
  }
}
func (BIT BinaryIndexedTree) Query(s, t int) int {
  return BIT.Get(t) - BIT.Get(s)
}
func (BIT *BinaryIndexedTree) Increment(idx int) {
  N := len((*BIT))
  for i := idx; i < N; i += i & (-i) {
    (*BIT)[i]++
  }
}
func InitBIT(N int) BinaryIndexedTree {
  BIT := make([]int, N + 1)
  return BIT
}

type Sequence [][]int
func (S Sequence) Len() int { return len(S) }
func (S Sequence) Less(i, j int) bool {
  if S[i][0] == S[j][0] { return S[i][1] < S[j][1] }
  return S[i][0] < S[j][0]
}
func (S Sequence) Swap(i, j int) { S[i], S[j] = S[j], S[i] }

func Inversion(A []int) int {
  N := len(A)
  BIT := InitBIT(N)
  I := 0
  B := make([][]int, N)
  for i, a := range A {
    B[i] = []int{a, i + 1}
  }
  sort.Sort(Sequence(B))
  for _, b := range B {
    I += BIT.Query(b[1], N)
    BIT.Update(b[1], 1)
  }
  return I
}

func Solve() {
  S := NextLine()
  N := len(S)
  C := make([]int, 26)
  for _, s := range S {
    C[int(s) - int('a')]++
  }
  if 1 < SumOdd(C...) {
    Write(-1)
    return
  }
  T := make([]int, N)
  P := make([]int, 26)
  Q := make([][]int, 26)
  for i, c := range C {
    Q[i] = make([]int, c / 2)
  }
  cnt := 0
  for i, s := range S {
    n := int(s) - int('a')
    if 0 < C[n] % 2 && P[n] == C[n] / 2 {
      T[i] = N / 2
    } else if P[n] < C[n] / 2 {
      T[i] = cnt
      Q[n][P[n]] = cnt
      cnt++
    } else {
      T[i] = N - 1 - Q[n][C[n] - P[n] - 1]
    }
    P[n]++
  }
  Write(Inversion(T))
  return
}

func main() {
  Solve()
  Output()
}