您的位置:首页 > 其它

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

2015-10-28 22:00 543 查看

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15247 Accepted Submission(s): 9311


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each case, output the minimum inversion number on a single line.

[align=left]Sample Input[/align]

10
1 3 6 9 0 8 5 7 4 2

[align=left]Sample Output[/align]

16

[align=left]Author[/align]
CHEN, Gaoli

[align=left]Source[/align]
ZOJ Monthly, January 2003

[align=left]Recommend[/align]
Ignatius.L | We have carefully selected several similar problems for you: 1698 1540 1542 1255 2795

虽然在学线段树。。。但是感觉美必要为了用线段树而用线段树。。。
我知道树状数组的东西都可以用线段树来解决。。。。
不过还是觉得树状数组。。。实在是优美。。。所以这题还是用树状数组搞得。。

这题是问一个长度为n的循环数组中,逆序对最少的个数。。。
我们可以先用树状数组求出初始的数列的逆序对。。。
然后其他的可以通过递推得到。。。。
当a[i]从处于位置1而被放到最后的时候。。。
cnt = cnt -a[i]+n-a[i]+1;
然后取所有cnt的最大值就行。

/*************************************************************************
> File Name: code/hud/1394.cpp
> Author: 111qqz
> Email: rkz2013@126.com
> Created Time: 2015年10月28日 星期三 21时16分47秒
************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#include<cctype>

#define yn hez111qqz
#define j1 cute111qqz
#define ms(a,x) memset(a,x,sizeof(a))
using namespace std;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
typedef long long LL;
typedef double DB;
const int inf = 0x3f3f3f3f;
const int N=5E3+7;
int c
;
int n,a
;

int lowbit( int x)
{
return x&(-x);
}

void update ( int x,int delta)
{
// cout<<"a?"<<endl;
for ( int i = x ; i <= n ; i = i + lowbit(i)) c[i] = c[i] + delta;
}
int Sum( int x)
{
//   cout<<"b?"<<endl;
int res = 0 ;
for ( int i = x ;i >= 1; i = i - lowbit(i))
{
res = res + c[i];
}
return res;
}
int main()
{
#ifndef  ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while (scanf("%d",&n)!=EOF)
{
ms(c,0);
for ( int i = 1 ; i <= n ; i++)
{
scanf("%d",&a[i]);
a[i]++;
}
int cnt = 0 ;
int ans = inf;
//   puts("mmmmmmmiao?");
for ( int i = 1  ; i <= n ; i++)
{
update(a[i],1);
cnt = cnt + i - Sum(a[i]);
//    puts("whats wrong?");
}
//  printf("cnt::%d\n",cnt);
if (cnt<ans) ans = cnt;
for ( int i = 1 ; i <= n ; i++)
{
cnt =cnt -a[i]+n-a[i]+1;
if (cnt<ans&&cnt>0) ans = cnt;
//    printf("cnt:%d\n",cnt);
}
printf("%d\n",ans);
}

#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: