[原创]HDU 1394 Minimum Inversion Number [线段树->单点更新]【数据结构】

2016-11-13 16:27:43 Tabris_ 阅读数:237


博客爬取于2020-06-14 22:42:38
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/53149713


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394

--------------------------------------------------.

Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18798 Accepted Submission(s): 11367

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
ZOJ Monthly, January 2003

---------------------------------------------------.

题目大意:
就是有一个序列 ,这个序列可以循环,变成下列这么些个序列。
a1, a2, …, an-1, an (m = 0 - the initial seqence)
a2, a3, …, an, a1 (m = 1)
a3, a4, …, an, a1, a2 (m = 2)

an, a1, a2, …, an-1 (m = n-1)
然后对于每种序列 计算逆序对数
然后输出逆序对数最小的值

解题思路:
首先对于最初的序列求解逆序对数 ,只需要线段树or树状数组就行了
这里采取的是线段树维护

在每次把数据挂到树上之前 可以区间查询的方式计算这个值的逆序数 然后O(nn)遍历一遍就能知道整个序列的逆序数了
总复杂度是O(nlognnlogn)

然后他要求的序列一共有n个那样的话不能O(n2lognn^2logn) 这样复杂度实在太高了

然后想最后发现了有一个规律;
因为序列中的数就是0~n-1且新的序列是从旧的序列移过来的。
那么逆序数就会相应减少aia_i个 同时就增加了nai1n-a_i-1
这样的话就能够O(11)处理了

附本题代码
------------------------------------.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# include <bits/stdc++.h>

# define abs(x) (((x)>0)?(x):-(x))
# define lalal puts("*********")
# define Rep(a,b,c) for(int a=(b);a<=(c);a++)
# define Req(a,b,c) for(int a=(b);a>=(c);a--)
# define Rop(a,b,c) for(int a=(b);a<(c);a++)
# define s1(a) scanf("%d",&a)
typedef long long int LL;
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 9901;
/**************************************/
const int N = 10000+5;
struct node
{
int l,r;
int val;
int md()
{
return (l+r)>>1;
}
}tree[N<<2];
int a[N],ans;
# define ll (rt<<1)
# define rr (rt<<1|1)
# define mid (tree[rt].md())
void pushup(int rt)
{
tree[rt].val=tree[ll].val+tree[rr].val;
}
void build(int rt,int l,int r)
{
tree[rt].l=l,tree[rt].r=r;
if(l==r)
{
tree[rt].val=0;
return ;
}
build(ll,l,mid);
build(rr,mid+1,r);
pushup(rt);

return;
}

void update(int rt,int pos,int val)
{
if(tree[rt].l==tree[rt].r)
{
tree[rt].val+=val;
return;
}

if(pos<=mid)update(ll,pos,val);
else update(rr,pos,val);
pushup(rt);
return;
}

void query(int rt,int L,int R)
{
if(L<=tree[rt].l&&tree[rt].r<=R)
{
ans += tree[rt].val;
return;
}
if(R<=mid)
query(ll,L,R);
else if(L>mid)
query(rr,L,R);
else
{
query(ll,L,R);
query(rr,L,R);
}
return;
}

int main()
{
int n;
while(~s1(n))
{
build(1,0,n);

int sum = 0;
Rep(i,1,n)
{
s1(a[i]);
ans = 0;
query(1,a[i]+1,n);
sum+=ans;
update(1,a[i],1);
}

int mi = sum;
Rep(i,1,n)
{
sum+=n-a[i]-a[i]-1;
if(sum<mi)
mi=sum;
}
printf("%d\n",mi);
}
return 0;
}