Minimum Inversion Number

Minimum Inversion Number

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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 <stdio.h>
 2 #include <algorithm>
 3
 4 using namespace std;
 5
 6 int a[5005];
 7 struct Node{
 8     int l,r,num;
 9 }tree[50000];
10
11 void Build(int n,int x,int y){
12     tree[n].l = x;
13     tree[n].r = y;
14     tree[n].num = 0;
15     if(x == y){
16         return;
17     }
18     int mid = (x + y) / 2;
19     Build(2*n,x,mid);
20     Build(2*n+1,mid+1,y);
21 }
22
23 void Modify(int n,int x){
24     int l = tree[n].l;
25     int r = tree[n].r;
26     int mid = (l + r) / 2;
27     if(x == l && x == r){
28         tree[n].num = 1;
29         return;
30     }
31     if(x <= mid)    Modify(2*n,x);
32     else            Modify(2*n+1,x);
33     tree[n].num = tree[2*n].num + tree[2*n+1].num;
34 }
35
36 int Query(int n,int x,int y){
37     int l = tree[n].l;
38     int r = tree[n].r;
39     int mid = (l + r) / 2;
40     int ans = 0;;
41     if(x == l && y == r)
42         return tree[n].num;
43     if(x <= mid)   ans += Query(2*n,x,min(mid,y));
44     if(y > mid)    ans += Query(2*n+1,max(mid+1,x),y);
45     return ans;
46 }
47 int main(){
48     //freopen ("a.txt" , "r" , stdin ) ;
49     int n,sum,ans;
50     int i,j;
51
52     while(scanf("%d",&n) != EOF){
53         sum = 0;
54         Build(1,0,n);
55         for(i = 1;i <= n;i++){
56             scanf("%d",&a[i]);
57             Modify(1,a[i]);
58             sum += Query(1,a[i]+1,n);
59         }
60         ans = sum;
61         for(i = 1;i < n;i++){
62             sum = sum + (n - 1 - a[i]) - a[i];
63             if(sum < ans)
64                 ans = sum;
65         }
66         printf("%d\n",ans);
67     }
68 }

时间: 2024-10-10 02:58:35

Minimum Inversion Number的相关文章

Minimum Inversion Number 【线段数】

Problem DescriptionThe 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 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

HDU1394 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

HDOJ 1394 Minimum Inversion Number 求循环串的最小逆序数(暴力&amp;&amp;线段树)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14879    Accepted Submission(s): 9082 Problem Description The inversion number of a given number sequence a1, a2, ..., a

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 1394 Minimum Inversion Number 逆序数/树状数组

Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1394 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

hdu 1394 Minimum Inversion Number

题目链接:hdu 1394 Minimum Inversion Number 该题是求最小逆序对的扩展.可以使用树状数组来实现.对于$n$个数的序列$A$,其第$i$个数($i\in [0,n)$)的逆序数$r_i$可以表示为它的角标$i$减去在它之前且不大于它的数的个数.例如对序列A = {1,3,5,9,0,8,5,7,4,2}中的数,A[8] = 4.其逆序数$r_8 = 8 - 3 = 5$,第二个3表示三个在它前面且比它小的数:{1,3,0}.从而我们可以得到第$i$个数的逆序数公式:

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

[hdu1394]Minimum Inversion Number(树状数组)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18395    Accepted Submission(s): 11168 Problem Description The inversion number of a given number sequence a1, a2, ..., a

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