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. ThinkPHP框架模型连贯操作(八)

    原文:ThinkPHP框架模型连贯操作(八) Thinkphp的连贯操作使用起来也是很灵活: *可能这里有的mysql函数没全部罗列出来,大家可以举一反三,形式雷同 一.常用连贯操作 1.where ...

  2. 算法起步之Bellman-Ford算法

    原文:算法起步之Bellman-Ford算法 从这篇开始我们开始介绍单源最短路径算法,他是图算法之一,我们前面说的贪心,图的遍历,动态规划都是他的基础,单源最短路径其实说的就是图中节点到节点的最短路径 ...

  3. OCP读书笔记(7) - 使用RMAN执行恢复

    7.Using RMAN to Perform Recovery 使用RMAN进行完全恢复system表空间文件丢失的恢复 模拟损坏: SQL> conn /as sysdba; SQL> ...

  4. POJ2599+POJ2082【最大矩形面积】

    题目链接:http://poj.org/problem?id=2559 题目链接:http://poj.org/problem?id=2082 这一类题目的解法,不知自己闲着没事就做了两个. 果然压栈 ...

  5. RFC2889转发性能測试用例设计和自己主动化脚本实现

    一.203_TC_FrameRate-1.tcl set chassisAddr 10.132.238.190 set islot 1 set portList {9 10} ;#端口的排列顺序是po ...

  6. A Game of Thrones(12) - Eddard

    The summons(['sʌm(ə)nz]召唤:传票) came in the hour before the dawn, when the world was still and grey. A ...

  7. 【C语言疯狂讲义】(八)C语言一维数组

    1.数组的基本概念: 同样类型    若干个     有序 由若干个同样类型的数据组成的有序的集合 有序:存储地址连续 下标连续 数组名:用来存放数组首地址的变量 数组元素:构成数组的每个数据 数组的 ...

  8. 在Windows通过使用MinGW静态编译Assimp

    使用MinGW静态编译Assimp 到了5月份了.没有写一篇日志,于是自己从知识库里面拿出一篇文章充数吧.这次将要解说怎样在Windows下使用MinGW静态编译Assimp. Assimp是眼下比較 ...

  9. jvm理论

    三大流行jvm sun HotSpot ibm j9 BEA JRockit Oracle 会基于HotSpot整合 JRockit. jvm运行时数据区 java虚拟机所管理的内存将会包括以下几个运 ...

  10. WPF案例(二)模拟Apple OS 界面前后180度反转

    原文:WPF案例(二)模拟Apple OS 界面前后180度反转 我们在设计应用程序界面的时候,为了充分利用界面空间,住住需要灵活的界面布局方式,比如可以在界面正面空间上定义一个Chart,背面空间上 ...