本来以为是求逆序数,要用归并排序,而且还得用线段树动态维护神马的,睁大眼睛一看,我滴妈呀,n才5000,直接暴力过了~~
/*
* hdu1394/linux.cpp
* Created on: 2011-9-6
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 5010;
int num[MAXN], reserve[MAXN];
void work();
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
work();
return 0;
}
void work() {
int N, totalres, temp;
while (scanf("%d\n", &N) == 1) {
for (int i = 0; i < N; i++) {
scanf("%d", &num[i]);
}
totalres = 0;
memset(reserve, 0, sizeof(reserve));
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if (num[i] > num[j]) {
reserve[i]++;
}
}
totalres += reserve[i];
}
temp = totalres;
for (int i = 0; i < N; i++) {
temp += N - 2 * reserve[i] - 1;
if (temp < totalres) {
totalres = temp;
}
for (int j = i + 1; j < N; j++) {
if (num[j] > num[i]) {
reserve[j]++;
}
}
}
printf("%d\n", totalres);
}
}