# hdu 1394 Minimum Inversion Number

hdu 1394 的传送门

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

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=5005;
int num[maxn];
struct
{
int l, r, num;
} tree[4*maxn];

void build(int root, int l, int r)
{
tree[root].l=l;
tree[root].r=r;
tree[root].num=0;
if(l == r)
return;
int mid=(l + r)>>1;
build(root<<1, l, mid);
build(root<<1|1, mid+1, r);
}

void update(int root, int pos)
{
if(tree[root].l == tree[root].r)
{
tree[root].num++;
return;
}
int mid=(tree[root].l+tree[root].r)>>1;
if(pos<=mid)
update(root<<1, pos);
else
update(root<<1|1, pos);
tree[root].num=tree[root<<1].num+tree[root<<1|1].num;
}

int query(int root, int L, int R)
{
if(L <= tree[root].l && R >= tree[root].r)
return tree[root].num;
int mid=(tree[root].l+tree[root].r)/2,ret=0;
if(L <= mid)
ret += query(2*root, L, R);
if(R>mid)
ret+=query(2*root+1, L, R);
return ret;
}
int main()
{

int m;
while(~scanf("%d",&m))
{
int sum=0;
build(1, 0, m);
for(int i=1; i<=m; i++)
{
scanf("%d",&num[i]);
update(1, num[i]);
sum+=query(1, num[i]+1, m);
}
int ans=sum;
for(int i=1; i<m; i++)
{
sum+=(m-1-num[i])-num[i];
if(sum<ans)
ans=sum;
}
printf("%d\n",ans);
}
return 0;
}

|

LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
111 0
|

HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
126 0
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
111 0
HDOJ(HDU) 1562 Guess the number(水题，枚举就行)
HDOJ(HDU) 1562 Guess the number(水题，枚举就行)
113 0
|
Java
HDU 1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 115124    Accepted Su...
797 0
|

KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11606    Accepted Submission...
811 0
|

Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 294    Accepted Submission(s):...
829 0
|
10月前
|

Leetcode 313. Super Ugly Number

89 1
|
3月前
|

LeetCode 题目 65：有效数字（Valid Number）【python】
LeetCode 题目 65：有效数字（Valid Number）【python】
41 5