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

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

Sample Output
  
  
16
 

Author
CHEN, Gaoli
 

Source
 

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;
}
/*
10
1 3 6 9 0 8 5 7 4 2
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值