HDU 1394 Minimum Inversion Number 线段树求最小逆序数

来源:互联网 发布:淘宝新店铺的扶持 编辑:程序博客网 时间:2024/06/05 23:06

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15368    Accepted Submission(s): 9372


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.
 

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
101 3 6 9 0 8 5 7 4 2
 

Sample Output
16
 

Author
CHEN, Gaoli
 

Source
ZOJ Monthly, January 2003
 

Recommend

Ignatius.L   |   We have carefully selected several similar problems for you:  1698 1540 1542 1255 1754 

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈#include <map>#include <set>#include <queue>#include <cmath>#include <stack>#include <cctype>#include <cstdio>#include <cstring>#include <stdlib.h>#include <iostream>#include <algorithm>#define rd(x) scanf("%d",&x)#define rd2(x,y) scanf("%d%d",&x,&y)#define rds(x) scanf("%s",x)#define rdc(x) scanf("%c",&x)#define ll long long int#define maxn 100005#define mod 1000000007#define INF 0x3f3f3f3f //int 最大值#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)#define MT(x,i) memset(x,i,sizeof(x))#define PI  acos(-1.0)#define E  exp(1)using namespace std;int t[maxn<<2],a[maxn],n,ans;void creatTree(int st,int left,int right){    t[st]=0;    if(left==right)return;    int m=(left+right)>>1;    int temp=st<<1;    creatTree(temp,left,m);    creatTree(temp+1,m+1,right);}void updata(int data,int st,int left,int right){    if(left==right){t[st]++;return;}    int m=(left+right)>>1;    int temp=st<<1;    if(data<=m)updata(data,temp,left,m);    else updata(data,temp+1,m+1,right);    t[st]=t[temp]+t[temp+1];}int qurey(int st,int l,int r,int left,int right){    if(l<=left&&r>=right)return t[st];    int m=(left+right)>>1;    int temp=st<<1;    int ret=0;    if(l<=m)ret+=qurey(temp,l,r,left,m);    if(r>m)ret+=qurey(temp+1,l,r,m+1,right);    return ret;}int main(){    while(rd(n)!=EOF){        creatTree(1,0,n-1);        ans=0;        FOR(i,0,n-1){            rd(a[i]);            ans+=qurey(1,a[i],n-1,0,n-1);            updata(a[i],1,0,n-1);        }        int temp=ans;        FOR(i,0,n-1){            temp=temp-(a[i]<<1)+n-1;            ans=ans>temp?temp:ans;        }        printf("%d\n",ans);    }    return 0;}/*101 3 6 9 0 8 5 7 4 2*/


0 0
原创粉丝点击