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 C (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

int cmp(const void* a, const void* b) {
	int x = *((const int*)a);
	int y = *((const int*)b);
	return x > y ? 1 : (x < y ? -1 : 0);
}

int N;
int A[200000];
int sort_temp[200000];

int get_id(int num) {
	int left = 0, right = N - 1;
	while (left <= right) {
		int mid = (left + right) / 2; /* どっかのサイトでは間違ってると主張されてるってやつ */
		if (sort_temp[mid] == num) return mid;
		else if (sort_temp[mid] < num) left = mid + 1;
		else right = mid - 1;
	}
	puts("Asumisu Kekkonn!!!");
	exit(1);
}

#define BIT_MAX 200000

int bit_table[BIT_MAX];

void bit_init(void) {
	int i;
	for(i=0;i<BIT_MAX;i++)bit_table[i]=0;
}

void bit_add(int pos,int value) {
	pos++;
	while(pos<=BIT_MAX) {
		bit_table[pos-1]+=value;
		pos+=pos & (-pos);
	}
}

int bit_sum(int pos) {
	int sum=0;
	pos++;
	while(pos>0) {
		sum+=bit_table[pos-1];
		pos-=pos & (-pos);
	}
	return sum;
}

int main(void) {
#if 0
	int i, j;
	uint64_t answer;
#endif
	int i;
	int gu_ki, ki_gu;
	if (scanf("%d", &N) != 1) return 1;
	for (i = 0; i < N; i++) {
		if (scanf("%d", &A[i]) != 1) return 1;
	}

#if 0
	/* とりあえず奇数と偶数にわけてソートするぞ */
	/* ソートを書くのは面倒くさいぞ */
	/* 偶数 */
	for (i = 0, j = 0; i < N; i += 2, j++) sort_temp[j] = A[i];
	qsort(sort_temp, j, sizeof(int), cmp);
	for (i = 0, j = 0; i < N; i += 2, j++) A[i] = sort_temp[j];
	/* 奇数 */
	for (i = 1, j = 0; i < N; i += 2, j++) sort_temp[j] = A[i];
	qsort(sort_temp, j, sizeof(int), cmp);
	for (i = 1, j = 0; i < N; i += 2, j++) A[i] = sort_temp[j];
#endif
	/* 座標圧縮するぞ */
	for (i = 0; i < N; i++) sort_temp[i] = A[i];
	qsort(sort_temp, N, sizeof(int), cmp);

#if 0
	/* バブルソートの交換回数するぞ */
#ifdef NAIVE_9D5D05CF_E86C_4690_BEB6_AB1B261D60BA
	answer = 0;
	for (i = N - 1; i >= 0; i--) {
		for (j = 0; j < i; j++) {
			if (A[j] > A[j + 1]) {
				int temp = A[j];
				A[j] = A[j + 1];
				A[j + 1] = temp;
				answer++;
			}
		}
	}
#else
	bit_init();
	answer = 0;
	for (i = 0; i < N; i++) {
		/* 先頭まで行く手間 - これより前にある自分より小さいのの数 */
		answer += i - bit_sum(get_id(A[i]));
		bit_add(get_id(A[i]), 1);
	}
#endif

	printf("%"PRIu64"\n", answer);
#endif

	/* カウントするぞ */
	gu_ki = ki_gu = 0;
	for (i = 0; i < N; i++) {
		if (i % 2 == 0 && get_id(A[i]) % 2 != 0) gu_ki++;
		if (i % 2 == 1 && get_id(A[i]) % 2 != 1) ki_gu++;
	}
	if (gu_ki != ki_gu) {
		/* 偶数から奇数に来た奴と奇数から偶数に来た奴の数が一致しない */
		puts("impossible?");
		return 1;
	}
	printf("%d\n", gu_ki);

	return 0;
}

/*
操作2 = 1個あけた2個の要素の順番を反転

ABABABABABABABABABABAB
操作2ではこのAとBの間で入れ変えることはできない

操作1だけならバブルソートの交換回数→大きいのが前にあるほど不利

→WA
反例

6
1
10
2
20
3
30

1 10 2 20 3 30 → バブルソートの交換回数は3、奇数偶数それぞれソート済み
1 10 2 3 20 30 → 操作1
1 3 2 10 20 30 → 操作2
1 2 3 10 20 30 → 操作1 → 2回でできる

新方式 : 奇数に来るはずのが偶数、偶数に来るはずのが奇数に来てたらカウント
1 10 2 20 3 30
0 4  1 5  2 3 ← 座標圧縮
0 1  2 3  4 5 ← 位置
o x  x o  o o ← 偶奇が一致?

操作2でグループ内どこにでも移動できるから、隣合わせにして交換できる

*/