Problem:
Score : 600 points

Problem Statement

Snuke got an integer sequence of length N from his mother, as a birthday present. The i-th (1 ≦ i ≦ N) element of the sequence is a_i. The elements are pairwise distinct.
He is sorting this sequence in increasing order.
With supernatural power, he can perform the following two operations on the sequence in any order:

Operation 1: choose 2 consecutive elements, then reverse the order of those elements.

Operation 2: choose 3 consecutive elements, then reverse the order of those elements.

Snuke likes Operation 2, but not Operation 1. Find the minimum number of Operation 1 that he has to perform in order to sort the sequence in increasing order.

Constraints

1 ≦ N ≦ 10^5

0 ≦ A_i ≦ 10^9

If i ≠ j, then A_i ≠ A_j.

All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of times Operation 1 that Snuke has to perform.

Sample Input 1

4
2
4
3
1

Sample Output 1

1

The given sequence can be sorted as follows:

First, reverse the order of the last three elements. The sequence is now: 2,1,3,4.

Then, reverse the order of the first two elements. The sequence is now: 1,2,3,4.

In this sequence of operations, Operation 1 is performed once. It is not possible to sort the sequence with less number of Operation 1, thus the answer is 1.

Sample Input 2

5
10
8
5
3
2

Sample Output 2

0

Solution in Go (1.6) (judge verdict: Time Limit Exceeded)

package main

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

func readQuestion() []int {

    bufio := bufio.NewReader(os.Stdin)
    l, _ := bufio.ReadString('\n')
    n, _ := strconv.Atoi(strings.Trim(l, "\n"))
    cards := make([]int, n)
    for i := 0; i < n; i++ {
        l, _ = bufio.ReadString('\n')
        n, _ := strconv.Atoi(strings.Trim(l, "\n"))
        cards[i] = n
    }
    return cards
}

func bubbleSort(vals []int, start int, step int) int {

    count := 0
    for end := len(vals) - step; end >= 0; end-- {
        for i := start; i < end; i += step {
            v1 := vals[i]
            v2 := vals[i+step]
            if v1 > v2 {
                vals[i], vals[i+step] = v2, v1
                count++
            }

        }
    }
    return count
}

func solve(vals []int) int {

    // bubble 2
    //fmt.Printf("%v\n", vals)
    bubbleSort(vals, 0, 2)
    //fmt.Printf("%v\n", vals)
    bubbleSort(vals, 1, 2)
    //fmt.Printf("%v\n", vals)

    ans := bubbleSort(vals, 0, 1)
    //fmt.Printf("%v\n", vals)
    return ans
}

func main() {
    vals := readQuestion()
    ans := solve(vals)
    fmt.Printf("%v\n", ans)
}