设为首页 加入收藏

TOP

hdu 1394 求循环串的最小逆序数 暴力法 线段树 归并排序3种方法 (一)
2015-11-21 01:21:27 来源: 作者: 【 】 浏览:10
Tags:hdu 1394 循环 最小 序数 暴力 线段 归并 排序 方法

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

?

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
?


Recommend
Ignatius.L
?
http://acm.hdu.edu.cn/showproblem.php?pid=1394
题意:
一个由0..n-1组成的序列,每次可以把队首的元素移到队尾,
????????? 求形成的n个序列中最小逆序对数目
?
思路:
如果求出第一种情况的逆序列,其他的可以通过递推来搞出来,一开始是t[1],t[2],t[3]....t[N]

它的逆序列个数是N个,如果把t[1]放到t[N]后面,逆序列个数会减少t[1]个,相应会增加N-(t[1]+1)个?


?

暴力法300ms:

[cpp]
#include??
int a[5555];?
int main()?
{?
??? int n,i,j,ans=999999999;?
??? while(scanf("%d",&n)!=EOF)?
??? {?
??????? ans=999999999;?
?????????? for(i=0;i ?????????? int cnt=0;?
?????????? for(i=0;i ?????????????? for(j=i+1;j ?????????????? {?
?????????????????? if(a[i]>a[j]) cnt++;?
?????????????? }?
?????????? // printf("cnt=%d\n",cnt);??
?????????? if(ans>cnt)? ans=cnt;?
?????????? for(i=0;i ?????????? {?
?????????????? cnt=cnt-a[i]+n-1-a[i];?
?????????????? if(ans>cnt)? ans=cnt;?
??????????? }?
?????????? printf("%d\n",ans);?
??? }?
??? return 0;?
}?

#include
int a[5555];
int main()
{
??? int n,i,j,ans=999999999;
??? while(scanf("%d",&n)!=EOF)
??? {
??????? ans=999999999;
?????????? for(i=0;i ?????????? int cnt=0;
?????????? for(i=0;i ?????????????? for(j=i+1;j ?????????????? {
?????????????????? if(a[i]>a[j]) cnt++;
?????????????? }
?????????? // printf("cnt=%d\n",cnt);
?????????? if(ans>cnt)? ans=cnt;
?????????? for(i=0;i ?????????? {
?????????????? cnt=cnt-a[i]+n-1-a[i];
?????????????? if(ans>cnt)? ans=cnt;
??????????? }
?????????? printf("%d\n",ans);
??? }
??? return 0;
}

下面说一下线段树的做法? 31ms
用线段树去求输入序列的逆序数
方法:
把树的叶子节点作为每个数的对应位置
枚举到第i个数时,我们需要求出前i次插入的数中有多少个比a[i]大,
即去寻找已经插入的数中比a[i]大的数的个数? 即查询叶子节点a[i]到n的数的个数

[cpp]
#include??
int a[10000];?
struct haha?
{?
??? int left;?
??? int right;?
??? int num;?
}node[10000*4];?
void build(int left,int right,int nd)?
{?
??? node[nd].left=left;?
??? node[nd].right=right;?
??? node[nd].num=0;?
??? if(left==right)??
??? {?
??????? return ;?
??? }?
??? int mid=(left+right)/2;?
??? build(left,mid,nd*2);?
??? build(mid+1,right,nd*2+1);?
}?
int query(int left,int right,int nd)?
{?
??? int mid=(node[nd].left+node[nd].right)/2;?
??? if(node[nd].left==left&&node[nd].right==right)?
??? {?
??????? return node[nd].num;?
??? }?
?
??? if(right<=mid)?
??? {?
????????? return query(left,right,nd*2);?
??? }?
??? else if(left>mid)?
??? {?
??????? return query(left,right,nd*2+1);?
??? }?
??? else?
??? {?
??????? return query(left,mid,nd*2)+query(mid+1,right,nd*2+1);?
??? }?
}?
void update(int pos,int nd)?
{?

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 4781 Assignment For Princes.. 下一篇hdu - 1829 - A Bug's Life

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: