首页 > 其他 > 详细

HDU 1394 线段树or 树状数组~

时间:2014-10-03 15:48:45      阅读:180      评论:0      收藏:0      [点我收藏+]

                              Minimum Inversion Number  

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
题目大意就是给你 N 个区间,里面包含有0~N-1的整数,允许你每次将序列的头调到序列尾,让你求出最小的逆序和,
思路:先用线段树or树状数组预处理出原先的逆序和,然后再一个一个推出,每次操作后所产生的的新的逆序和,就拿样例来说,一开始的逆序和是22,如果将1调到末尾那里的话,原先在1后面比1小的数有1个,这个数是0,而当1调到后面的时候,在1前面的比一大的数有8个,为3,6,9,8,5,7,4,2,那么新序列的逆序和为22 - 1 + 8 = 29。以此类推
 
AC代码:线段树的方法,借鉴了notonlysuccess大神的姿势:
Memory: 320 KB   Time: 78 MS
bubuko.com,布布扣
#include <cstdio>
#include <iostream>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 5555;
int sum[maxn<<2];
void PushUp(int rt){
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void build(int l,int r,int rt){
    sum[rt] = 0;
    if(l == r) return ;
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    return ;
}
void update(int p,int l,int r,int rt){
    if(l == r){
        sum[rt]++;
        return ;
    }
    int m = (l + r)>>1;
    if(p <= m) update(p,lson);
    else update(p,rson);
    PushUp(rt);
}
int query(int L,int R,int l,int r,int rt){
    if(L <= l&&r <= R){
        return sum[rt];
    }
    int m = (l + r)>>1;
    int ret = 0;
    if(L <= m) ret += query(L,R,lson);
    if(R > m)  ret += query(L,R,rson);
    return ret;
}
int x[maxn];
int main(){
    int n;
    while(~scanf("%d",&n)){
        build(0,n - 1,1);
        int sum = 0;
        for(int i = 0;i < n;i++){
            scanf("%d",&x[i]);
            sum += query(x[i],n - 1,0,n - 1,1);
            update(x[i],0,n - 1,1);
        }
        int res = sum;
        for(int i = 0;i < n;i++){
            sum += n - x[i] - x[i] - 1;
            res = min(res,sum);
        }
        printf("%d\n",res);
    }
    return 0;
}
View Code

后来我有用树状数组做了一下,发觉树状数组一般都要比线段树要快~

Memory: 332 KB   Time: 31 MS
bubuko.com,布布扣
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 5555;
int c[maxn];
int a[maxn];
int n;
inline int lowbit(int x){
    return x&(-x);
}
int sum(int x){
    int ret = 0;
    while(x > 0){
        ret += c[x];x -= lowbit(x);
    }
    return ret;
}
void add(int x,int d){
    while(x <= n){
        c[x] += d;x += lowbit(x);
    }
}
int main(){
    while(~scanf("%d",&n)){
        memset(c,0,sizeof(c));
        memset(a,0,sizeof(a));
        int ans = 0;
        for(int i = 1;i <= n;i++){
            scanf("%d",&a[i]);
            ans += i - 1 - sum(a[i]);
            add(a[i] + 1,1);
        }
        //cout<<ans<<endl;
        int MIN = ans;
        for(int i = 1;i <= n;i++){
            MIN += n - (a[i] + 1) - (a[i]);
            ans = min(MIN,ans);
        }
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

 

 

HDU 1394 线段树or 树状数组~

原文:http://www.cnblogs.com/jusonalien/p/4004973.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!