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): 17675    Accepted Submission(s): 10721


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
 题意:
给一个序列,可以将任意数量的数后置,求最少的逆序数对数。
思想:
可能看到5000的范围,直接暴力能过,但我个人觉得此题没考暴力,题目给了逆序数的范围,所以,简明看出逆序数性质+线段树动态插点更新,求前面插入的数中比该数大的个数。可能大家还没懂意思。下面给出AC代码,可以通过代码看出:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<stack>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define memmax(a) memset(a,0x3f,sizeof(a))
#define pfn printf("\n")
#define ll __int64
#define mod 1000000007
#define sf(a) scanf("%d",&a)
#define sf64(a) scanf("%I64d",&a)
#define sf2(a,b) scanf("%d%d",&a,&b)
#define sf3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define sff(a) scanf("%f",&a)
#define sfs(a) scanf("%s",a)
#define sfs2(a,b) scanf("%s%s",a,b)
#define sfs3(a,b,c) scanf("%s%s%s",a,b,c)
#define sfc(a) scanf("%c",&a)
#define str(a) strlen(a)
#define debug printf("***\n")
const double PI = acos(-1.0);
const double e = exp(1.0);
const int INF = 0x7fffffff;;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
bool cmpbig(int a, int b){ return a>b; }
bool cmpsmall(int a, int b){ return a<b; }
using namespace std;
#define MAX  1000010
int a[MAX];
struct arnode
{
	int left;
	int right;
	int num;
}node[MAX];
void maintain(int root)
{
	int lr=root<<1,rr=(root<<1)+1;
	node[root].num=node[lr].num+node[rr].num;
	return ;
}
void build(int root,int l,int r)
{
	node[root].left=l;node[root].right=r;
	node[root].num=0;
	if(l==r)
		return ;
	int mid=(l+r)>>1;
	build(root<<1,l,mid);
	build((root<<1)+1,mid+1,r);
	//maintain(root);
}
int query(int root,int l,int r)
{
	if(node[root].left==l&&node[root].right==r)
	{
		return node[root].num;
	}
	int mid=(node[root].left+node[root].right)>>1;
	if(l>=mid+1)
		return query((root<<1)+1,l,r);
	else if(r<=mid)
		return query(root<<1,l,r);
	else
		return query(root<<1,l,mid)+query((root<<1)+1,mid+1,r);
}
void updata(int root,int pos)
{
	if(node[root].left==node[root].right)
	{
		//printf("%d %d %d\n",root,node[root].left,node[root].right);
		node[root].num++;
		//printf("%d \n",node[root].num);
		return ;
	}
	int mid=(node[root].left+node[root].right)>>1;
	if(pos<=mid)
		updata(root<<1,pos);
	else
		updata((root<<1)+1,pos);
	maintain(root);
}
int main()
{
	//freopen("data.in","r",stdin);
	int n;
	while(~sf(n))
	{
		int i,j;
		for(i=0;i<n;i++)
			sf(a[i]);
		build(1,0,n-1);
		ll cnt=0,pos;
		for(i=0;i<n;i++)
		{
			cnt+=query(1,a[i],n-1);
			//printf("%d\n",a[i]+1);
			updata(1,a[i]);
		}
		/*for(i=1;i<=20;i++)
			printf("%d %d %d %d\n",i,node[i].left,node[i].right,node[i].num);*/
		//printf("%I64d\n",cnt);
		pos=cnt;
		for(i=0;i<n;i++)
		{
			cnt=cnt-a[i]+(n-1-a[i]);
			if(cnt<pos)
				pos=cnt;
		}
		printf("%I64d\n",pos);
	}
	return 0;
}
刚开始写时,疯狂wa,最后发现,自己手残 int型忘return,吃一堑长一智。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值