HDU 1394:Minimum Inversion Number(线段树区间求和单点更新)

http://acm.hdu.edu.cn/showproblem.php?pid=1394          

Minimum Inversion Number

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

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 #define N 5005
 6 #define lson rt<<1,l,m
 7 #define rson rt<<1|1,m+1,r
 8 struct node
 9 {
10     int l,r,value;
11 }tree[N<<2];
12 int a[N];
13 /*
14 求移位后的最小逆序数
15 Update单点增减
16 Query区间求和
17 */
18 void Build(int rt,int l,int r)
19 {
20     tree[rt].l=l;
21     tree[rt].r=r;
22     tree[rt].value=0;
23     if(l==r) return ;
24     int m=(l+r)>>1;
25     Build(rt<<1,l,m);
26     Build(rt<<1|1,m+1,r);
27 }
28
29 void Update(int rt,int num)
30 {
31     if(tree[rt].l==num&&tree[rt].r==num){
32         tree[rt].value=1;
33         return ;
34     }
35     int m=(tree[rt].l+tree[rt].r)>>1;
36     if(num<=m) Update(rt<<1,num);
37     else Update(rt<<1|1,num);
38     tree[rt].value=tree[rt<<1].value+tree[rt<<1|1].value;
39 }
40
41 int Query(int rt,int l,int r)
42 {
43 /*
44 画个图就明白了,搜寻的区间为[l,r],m代表当前节点的左儿子和右儿子的分支
45 如果左儿子有包含该区间的元素,即l<=m,那么就要继续递归搜寻左儿子
46 如果右儿子有包含该区间的元素,即m<r,那么就要继续递归搜寻右儿子
47 当当前的节点的区间被搜寻的区间[l,r]覆盖的话,就说明该段区间完全在[l,r]里面
48 那么返回该节点的值
49 */
50     if(l<=tree[rt].l&&tree[rt].r<=r){
51         return tree[rt].value;
52     }
53     else{
54         int m=(tree[rt].l+tree[rt].r)>>1;
55         int s1=0,s2=0;
56         if(l<=m) s1=Query(rt<<1,l,r);
57         if(r>m) s2=Query(rt<<1|1,l,r);
58         return s1+s2;
59     }
60 }
61
62 int main()
63 {
64     int n;
65     while(~scanf("%d",&n)){
66         Build(1,1,n);
67         int sum=0;
68         for(int i=0;i<n;i++){
69             scanf("%d",&a[i]);
70             a[i]++;
71             sum+=Query(1,a[i]+1,n);
72             Update(1,a[i]);
73         }
74         int res = sum;
75         for(int i=n-1;i>=0;i--){
76             sum=sum-(n-a[i])+(a[i]-1);
77             if(sum<res) res=sum;
78         }
79         printf("%d\n",res);
80     }
81     return 0;
82 }
时间: 2024-08-09 23:53:29

HDU 1394:Minimum Inversion Number(线段树区间求和单点更新)的相关文章

hdu 1394 Minimum Inversion Number 线段树 点更新

// hdu 1394 Minimum Inversion Number 线段树 点更新 // // 典型线段树的单点更新 // // 对于求逆序数,刚开始还真的是很年轻啊,裸的按照冒泡排序 // 求出最初始的逆序数,然后按照公式递推,结果就呵呵了 // // 发现大牛都是用线段树和树状数组之类的做的,而自己又在学 // 线段树,所以就敲了线段树. // // 线段树的节点保存一段区间( L,R )内0,1...n一共出现了多少个. // 因为每个数是0,1,2...n-1且没有重复的数字. /

HDU 1394 Minimum Inversion Number.(线段树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 ~~~~ 早起一发线段树,开心又快乐.这题暴力也能水过,同时线段树的效率也就体现的尤为明显了,看了大牛的博客,说是还可以用树状数组,点树和合并序列写,现在还不懂,留着以后在写吧. ~~~~ 大致题意:给定一个数字序列,同时由此可以得到n个序列, 要求从n个序列中找到逆序数最小的序列,输出最小逆序数. 首先介绍下逆序数的概念: 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面

hdu - 1394 Minimum Inversion Number(线段树水题)

http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum就是当前数的逆序数. 这样查询完之后得到初始数列的逆序数,要求得所有序列的最小逆序数,还需要循环一次. 设初始序列abcde中逆序数为k,小于a的个数是t-1那么大于a的个数就是n-t,当把a左移一位,原来比a大的都变成了a的逆序对,即逆序数增加了n-t,但是原来比a小的数都变成了顺序, 因此逆序数

HDU 1394 Minimum Inversion Number (线段树,单点更新)

C - Minimum Inversion Number Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1394 Appoint description: System Crawler (2015-08-17) Description The inversio

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): 11981    Accepted Submission(s): 7321 Problem Description The inversion number of a given number sequence a1, a2, ..., a

HDU 1394 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 seq

hdu 1394 Minimum Inversion Number 线段树

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

hdu 1116 敌兵布阵 线段树 区间求和 单点更新

线段树的基本知识可以先google一下,不是很难理解 线段树功能:update:单点增减 query:区间求和 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 using namespace std; const int MAXN = 50008; int sum[MAXN<<2]; void build(int l, int r, int rt)

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列: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)求这些序列中,逆序数最少的