Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18395    Accepted Submission(s): 11168

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
 
Recommend
Ignatius.L
 
 
更更更
 
隔壁YNY看到就直接暴力,当然T了(哈哈哈一起笑他)
动动脑子,题目说是一个环
那么每转一次,就相当于把第一个数放到最后面
考虑第一个数对原有答案的贡献是a[1]-1,也就是小于它的个数(数据是1到n的排列)
最后一个数对原答案的贡献相反
那么移动后当前逆序对数就要减去比第一个数小的个数,再加上比它大的数的个数
这样我们求出一次移动后的逆序对数
这时候我们发现下一次移动直接修改答案就好,不需要改动树状数组
推出式子ans+=n-a[i]-(a[i]-1)
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LL long long
int bit[]={},n,a[];
inline LL min(LL a,LL b){
return a<b?a:b;
}
inline int lb(int x){
return x&(-x);
}
inline LL q(int x){
LL ans=;
while(x){
ans+=bit[x];
x-=lb(x);
}
return ans;
}
inline int c(int x){
while(x<=n){
bit[x]++;
x+=lb(x);
}
return ;
}
int main(){
while(scanf("%d",&n)!=EOF){
memset(bit,,sizeof(bit));
LL ans=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
a[i]++;
ans+=q(n)-q(a[i]);
c(a[i]);
}
LL mn=ans;
mn=min(mn,ans);
for(int i=;i<=n;i++){
ans+=n-a[i]-(a[i]-);
mn=min(mn,ans);
}
printf("%lld\n",mn);
} return ;
}
 
 
 
 
 
 
 
 
 
 

[hdu1394]Minimum Inversion Number(树状数组)的更多相关文章

  1. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  2. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

  3. hdu 1394 Minimum Inversion Number - 树状数组

    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...

  4. HDU 1394 Minimum Inversion Number (树状数组 && 规律 && 逆序数)

    题意 : 有一个n个数的数列且元素都是0~n-1,问你将数列的其中某一个数及其前面的数全部置到后面这种操作中(比如3 2 1 0中选择第二个数倒置就产生1 0 3 2)能产生的最少的逆序数对是多少? ...

  5. hdu1394(Minimum Inversion Number)线段树

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

  6. HDU-1394 Minimum Inversion Number (逆序数,线段树或树状数组)

    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...

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

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

  8. 2018.07.08 hdu1394 Minimum Inversion Number(线段树)

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

  9. HDU1394 Minimum Inversion Number(线段树OR归并排序)

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

随机推荐

  1. Sharepoint 2013 网站集的删除与还原

    一.可以通过三种方法删除网站集: 1.打开Sharepoint 2013 管理页面首页 ---> 单击‘应用程序管理(Application Management)’并进入该页面 ---> ...

  2. 使用 JavaScript 实现栈

    1.栈的基本操作 function Stack() { //使用数组保存栈元素 var items = []; //添加新元素到栈顶(相当于数组的末尾) this.push = function(el ...

  3. Yii源码阅读笔记(二十三)

    Module类中的辅助功能方法: /** * Returns an ID that uniquely identifies this module among all modules within t ...

  4. xampp使用phpunit

    1.将xampp/php的pear文件夹里面的phpunit文件夹复制到htdocs目录下 2.复制xampp/php的phpunit.bat到需要测试的目录 3.使用cmd命令切换至phpunit. ...

  5. sql操作之修改表结构

    修改表的语法=========================增加列[add 列名]=========================①alter table 表名 add 列名 列类型 列参数[加的 ...

  6. delphi之动态库调用和串口通讯

    串口通讯: Spcomm 控件属性: CommName  :表示COM1,COM2等串口的名字: BaudRate:设定波特率9600,4800等 StartComm StopComm 函数Write ...

  7. 【转】Ubuntu下查看软件版本及安装位置

    查看软件版本:aptitude show xxx 也可用apt-show-versions (要先安装sudo apt-get install apt-show-versions) 查看软件安装位置: ...

  8. lua module package.seeall选项

    module 与 package.seeall http://blog.codingnow.com/2006/02/lua_51_module.html 使用 module("test&qu ...

  9. DuiLib学习笔记2——写一个简单的程序

    我们要独立出来自己创建一个项目,在我们自己的项目上加皮肤这才是初衷.我的新建项目名为:duilibTest 在duilib根目录下面有个 Duilib入门文档.doc 我们就按这个教程开始入门 首先新 ...

  10. 隐藏,显示任务栏,桌面 C++代码

    HWND desktop,task; desktop=FindWindow(L"ProgMan",NULL); task=FindWindow(L"Shell_TrayW ...