Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 10326 Accepted Submission(s): 6359

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
题目大意,从初始的数组開始,每次把第一个加到最后一个,求逆序数,共求出n个逆序数,找出最小值
对每种情况都求逆序数,能够每次都归并排序,或树状数组,或线段树,也能够由上一个的逆序数推出;
a[1] , a[2] , a[3] 。。。a[n],将a[1]放到最后,然后将a[2]放到最后,能够找到规律,将首个a[i]放到最后时,逆序数添加了 a[i]之前比a[i]大的,添加a[i]之后比a[i]大的,减小了a[i]之前比a[i]小的,减小了a[i]之后比a[i]小的,又由于每次给出的数n个数在0到n,且都不同,最后得出 逆序数会添加 n-a[i]个,降低a[i]-1个
#include <cstdio>
#include <cstring>
#define INF 0x3f3f3f3f
#include <algorithm>
using namespace std;
int tree[100000] , p[6000] , q[6000];
void update(int o,int x,int y,int u)
{
if( x == y && x == u )
tree[o]++ ;
else
{
int mid = (x + y)/ 2;
if( u <= mid )
update(o*2,x,mid,u);
else
update(o*2+1,mid+1,y,u);
tree[o] = tree[o*2] + tree[o*2+1];
}
}
int sum(int o,int x,int y,int i,int j)
{
int ans = 0 ;
if( i <= x && y <= j )
return tree[o] ;
else
{
int mid = (x + y) /2 ;
if( i <= mid )
ans += sum(o*2,x,mid,i,j);
if( mid+1 <= j )
ans += sum(o*2+1,mid+1,y,i,j);
}
return ans ;
}
int main()
{
int i , j , n , min1 , num ;
while(scanf("%d", &n)!=EOF)
{
min1 = 0 ;
memset(q,0,sizeof(q));
for(i = 1 ; i <= n ; i++)
{
scanf("%d", &p[i]);
p[i]++ ;
}
memset(tree,0,sizeof(tree));
for(i = 1 ; i <= n ; i++)
{
min1 += sum(1,1,n,p[i],n);
update(1,1,n,p[i]);
}
num = min1 ;
for(i = 1 ; i < n ; i++)
{
num = num + ( n - p[i] ) - (p[i] - 1) ;
if( num < min1 )
min1 = num ;
}
printf("%d\n", min1);
}
return 0;
}

hdu1394--Minimum Inversion Number(线段树求逆序数,纯为练习)的更多相关文章

  1. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  2. HDU_1394_Minimum Inversion Number_线段树求逆序数

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  3. hdu1394(Minimum Inversion Number)线段树

    明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...

  4. HDU-1394 Minimum Inversion Number 线段树+逆序对

    仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...

  5. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  6. hdu 13394 Minimum Inversion Number 线段树

    题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...

  7. HDU-1394 Minimum Inversion Number(线段树求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  8. hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)

    题目传送门 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  9. HDU - 1394 Minimum Inversion Number (线段树求逆序数)

    Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs ( ...

  10. Minimum Inversion Number(线段树求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

随机推荐

  1. LCS最大公共子序列问题

    在生物应用中,经常需要比较两个(或多个)不同生物体的DNA, 例如:某种生物的DNA可能为S1=ACCGGTCGAGTGCGCGGAAGCCGGCCGAA, 另一种生物的DNA可能为S2=GTCGTT ...

  2. 2014辽宁省赛 Repeat Number

    问题 C: Repeat Number 时间限制: 1 Sec  内存限制: 128 MB [cid=1073&pid=2&langmask=0">提交][状态][论坛 ...

  3. POJ 2826 An Easy Problem?! 好的标题

    受该两块木板以形成槽的效果.Q槽可容纳雨水多,注意雨爆跌,思想是非常easy,分类讨论是有点差. 1.假定两条线段不相交或平行,然后再装0: 2.有一个平行x轴.连衣裙0. 3.若上面覆盖以下的,装0 ...

  4. Twitter僵尸帐号厂商雇佣中国员工专填验证码_Web2.0 - Microblogging 微博_cnBeta.COM

    Twitter僵尸帐号厂商雇佣中国员工专填验证码_Web2.0 - Microblogging 微博_cnBeta.COM Twitter僵尸帐号厂商雇佣中国员工专填验证码

  5. UVA 639 (13.08.25)

     Don't Get Rooked  In chess, the rook is a piece that can move any number of squaresvertically or ho ...

  6. Semaphore实现Andoird版源代码剖析

    Semaphore是一个计数的信号量.从概念上来说,信号量维持一组许可(permits).acquire方法在必须的时候都会堵塞直到有一个许可可用,然后就会拿走这个许可.release方法加入一个许可 ...

  7. Java 模拟队列(一般队列、双端队列、优先级队列)

    队列: 先进先出,处理类似排队的问题,先排的.先处理,后排的等前面的处理完了,再处理 对于插入和移除操作的时间复杂度都为O(1).从后面插入,从前面移除 双端队列: 即在队列两端都能够insert和r ...

  8. Wix学习整理(5)——安装时填写注册表

    原文:Wix学习整理(5)--安装时填写注册表 一 Microsoft操作系统的注册表 什么是注册表? 注册表是Mircrosoft Windows中的一个重要的数据库,用于存储系统和应用程序的设置信 ...

  9. 金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on Xanga

    金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on X ...

  10. hdu 1849 (尼姆博弈)

    http://acm.hdu.edu.cn/showproblem.php? pid=1849 简单的尼姆博弈: 代码例如以下: #include <iostream> #include ...