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. 检查java class的版本号

    补丁总是会一遍又一遍的打,越打越多 有时候,就担心有人不小心把高版本的class打到低版本jre运行的环境中 简单写了点代码,检查文件夹中class的版本号 package org.wee.cv; i ...

  2. QTableView 固定列宽度(鼠标拖动后,仍可固定)

    QTableView 提供一个函数: void QTableView::setColumnWidth ( int column, int width ) 用于设置column指定的列的宽度 但setC ...

  3. UVALive 6869(后缀数组)

    传送门:Repeated Substrings 题意:给定一个字符串,求至少重复一次的不同子串个数. 分析:模拟写出子符串后缀并排好序可以发现,每次出现新的重复子串个数都是由现在的height值减去前 ...

  4. Spring整合的quartz任务调度的实现方式

    一.在web.xml中将配置文件的位置指定好. Web.xml的配置如下: <?xmlversion="1.0"encoding="UTF-8"?> ...

  5. POJ 3414 Pots 记录路径的广搜

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  6. poj 3270 更换使用

    1.确定初始和目标状态. 明确.目标状态的排序状态. 2.得出置换群,.比如,数字是8 4 5 3 2 7,目标状态是2 3 4 5 7 8.能写为两个循环:(8 2 7)(4 3 5). 3.观察当 ...

  7. GitLab 5.3 升级注意事项

    最主要就是需要更新的Git.我的Ubuntu12.04通过apt-get install安装的git版本过低. 所以只能通过源代码安装. 参考下面的步骤: wget git-core.googleco ...

  8. git使用ssh密钥和https两种认证方式汇总(转)

    在版本库的SSH方式和HTTPS方式是不同的,具体来说就是url信息的不同,但是,实际的认证机制也是不同的.当建立了本机密钥之后,使用ssh方式实际上是不需要再次认证的,而https则每次需要输入密码 ...

  9. 端口映射工具 redir/socat/xinetd - 运维技术 - 开源中国社区

    端口映射工具 redir/socat/xinetd - 运维技术 - 开源中国社区 端口映射工具 redir/socat/xinetd    10人收藏此文章, 我要收藏 发表于3天前(2013-08 ...

  10. 墙体裂缝推荐的情况下驱动的PhoneGap入门,早看早收据

    清华大学出版社推出<构建跨平台APP:PhoneGap移动应用实战> 零门槛的学习APP发展 刮 进步 20以上示范样本APP 3项目APP 台à跨终端à移动开发 完美生命周期:搭建好开发 ...