Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16046 Accepted Submission(s): 9763
Problem Description
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.
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
/* hdu 1394 求a[i]前面小于a[i]的数的个数 给你一个序列,求每个a[i]后面小于a[i]的个数, 然后你可以把第一个数放到最后,这样的话sum变化:sum = sum+(n-1-a[i])-a[i]; hhh-2016-02-27 15:18:09 */ #include <iostream> #include <cstdio> #include <cstring> #include <ctime> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <vector> typedef long long ll; using namespace std; const int maxn = 200000+5; int a[maxn]; struct node { int l,r; int num; } tree[maxn<<2]; void push_up(int r) { int lson = r<<1,rson = (r<<1)|1; tree[r].num = (tree[lson].num+tree[rson].num); } void build(int i,int l,int r) { tree[i].l = l,tree[i].r = r; tree[i].num = 0; if(l == r) { return ; } int mid = (l+r)>>1; build(i<<1,l,mid); build(i<<1|1,mid+1,r); push_up(i); } void push_down(int r) { } void Insert(int i,int k) { if(tree[i].l == k && tree[i].r == k) { tree[i].num++; return ; } push_down(i); int mid = (tree[i].l + tree[i].r) >>1; if(k <= mid) Insert(i<<1,k); if(k > mid) Insert(i<<1|1,k); push_up(i); } int query(int i,int l,int r) { if(tree[i].l >= l && tree[i].r <= r) { return tree[i].num; } push_down(i); int mid = (tree[i].l+tree[i].r)>>1; int ans = 0; if(l <= mid) ans+=(query(i<<1,l,r)); if(r > mid) ans+=(query(i<<1|1,l,r)); return ans ; } int main() { int T,n,m,cas = 1; while(scanf("%d",&n)!=EOF) { build(1,0,n-1); int sum = 0; for(int i =1; i <= n; i++) { scanf("%d",&a[i]); Insert(1,a[i]); int t; if(a[i] - 1 < 0) t = 0; else t = query(1,0,a[i]-1); sum += (a[i] - t); } int ans = sum; for(int i = 1;i <= n;i++) { sum = sum+(n-1-a[i])-a[i]; ans = min(ans,sum); } printf("%d ",ans); } return 0; }