The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.
For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
Input
The input consists of a number of test cases. Each case consists of two lines:
the first line contains a positive integer n (n <= 5000);
the next line contains a permutation of the n integers from 0 to n-1.
Output
For each case, output the minimum inversion number on a single line.
Sample Input
10
1 3 6 9 0 8 5 7 4 2
Sample Output
16
题目大意 求循环排列最小逆序对数。
老是刷cf,不做暑假作业估计会被教练鄙视,所以还是做做暑假作业。
先用各种方法算出原始序列的逆序对数。
显然你可直接算出把开头的一个数挪到序列后面增加的逆序对数。(后面有多少个比它大减去有多少个比它小)
于是这道题就水完了。
Code
/**
* hdu
* Problem#1394
* Accepted
* Time: 62ms
* Memory: 1680k
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const signed long long llf = (signed long long)((1ull << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} #define lowbit(x) ((x) & (-x)) typedef class IndexedTree {
public:
int* lis;
int s;
IndexedTree() { }
IndexedTree(int s):s(s) {
lis = new int[(s + )];
memset(lis, , sizeof(int) * (s + ));
} inline void add(int idx, int val) {
for(; idx <= s; idx += lowbit(idx))
lis[idx] += val;
} inline int getSum(int idx) {
int ret = ;
for(; idx; idx -= lowbit(idx))
ret += lis[idx];
return ret;
}
}IndexedTree; int n;
int* arr;
inline boolean init() {
if(!readInteger(n)) return false;
arr = new int[(n + )];
for(int i = ; i <= n; i++)
readInteger(arr[i]), arr[i] += ;
return true;
} IndexedTree it;
int ans, cmp;
inline void solve() {
ans = , cmp = ;
it = IndexedTree(n);
for(int i = ; i <= n; i++) {
it.add(arr[i], );
// cout << 1;
cmp += i - it.getSum(arr[i]);
}
ans = cmp;
for(int i = ; i < n; i++) {
cmp -= arr[i] - , cmp += n - arr[i];
smin(ans, cmp);
}
printf("%d\n", ans);
} inline void clear() {
delete[] arr;
delete[] it.lis;
} int main() {
while(init()) {
solve();
clear();
}
return ;
}