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. touch修改文件的修改时间和访问时间,ls --full-time显示文件详细,stat命令

    1. 同时修改文件的修改时间和访问时间 touch -d "2010-05-31 08:10:30" test.doc 2. 只修改文件的修改时间 touch -m -d &quo ...

  2. 解决CentOS无法显示中文字体 | 系统运维 | Web2.0

    解决CentOS无法显示中文字体 | 系统运维 | Web2.0 About Me    博客园    devops    前端    张家港水蜜桃 傍晚好! 2013年09月12日 17:56:08 ...

  3. C++编程命名规范

    原地址:http://www.cnblogs.com/joinclear/archive/2013/02/21/2921422.html C++编程命名规范 0前言 根据多年工作经验和其它命名规范整理 ...

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

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

  5. Wix打包系列(四) 自定义UI

    原文:Wix打包系列(四) 自定义UI 除了标准的安装界面,如果我们要在安装时需要提供一些额外的信息时,这时就需要自定义界面来显示和录入这些信息. 4.1  自定义对话框 如上一章中我们测试数据库的连 ...

  6. 收集经常使用的.net开源项目

    Json.NET http://json.codeplex.com/ Json.Net是一个读写Json效率比較高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Lin ...

  7. BCM wifi分析

    一个:载入中wifi驱动模块 在hardware/libhardware_legacy/wifi/wifi.c调用函数 insmod(DRIVER_MODULE_PATH, DRIVER_MODULE ...

  8. Python什么是二次开发的意义?python在.net项目采用

    任何人都知道python在.net该项目是做什么的啊? 辅助用途,用作"二次开发"..net站点的话python主要是CGI才用.能够用python编写B/S程序. 解释一下二次开 ...

  9. SQL server 2000安装时“以前的某个程序安装已在安装计算机上创建挂起”

    客户使用的固定资产盘点软件使用的数据库是MSSQL 2000,在安装完成打上SP3的时候提示“以前的某个程序安装已在安装计算机上创建挂起的文件操作.运行安装程序之前必须重新启动计算机“,从客户的软件提 ...

  10. Projecet客户端登陆无法通过验证

    客户反映使用Project客户端登陆project服务器的时候,只有域管理员账户才能够登陆成功,其他的账户登陆都无法验证通过,无论是https的方式还是客户端的方式,但是域账户登陆计算机是可以登陆成功 ...