[hdu1394]Minimum Inversion Number(树状数组)
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
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.
1 3 6 9 0 8 5 7 4 2
#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(树状数组)的更多相关文章
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
- hdu 1394 Minimum Inversion Number - 树状数组
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- HDU 1394 Minimum Inversion Number (树状数组 && 规律 && 逆序数)
题意 : 有一个n个数的数列且元素都是0~n-1,问你将数列的其中某一个数及其前面的数全部置到后面这种操作中(比如3 2 1 0中选择第二个数倒置就产生1 0 3 2)能产生的最少的逆序数对是多少? ...
- hdu1394(Minimum Inversion Number)线段树
明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...
- HDU-1394 Minimum Inversion Number (逆序数,线段树或树状数组)
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- HDU-1394 Minimum Inversion Number 线段树+逆序对
仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...
- 2018.07.08 hdu1394 Minimum Inversion Number(线段树)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- HDU1394 Minimum Inversion Number(线段树OR归并排序)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- Sharepoint 2013 网站集的删除与还原
一.可以通过三种方法删除网站集: 1.打开Sharepoint 2013 管理页面首页 ---> 单击‘应用程序管理(Application Management)’并进入该页面 ---> ...
- 使用 JavaScript 实现栈
1.栈的基本操作 function Stack() { //使用数组保存栈元素 var items = []; //添加新元素到栈顶(相当于数组的末尾) this.push = function(el ...
- Yii源码阅读笔记(二十三)
Module类中的辅助功能方法: /** * Returns an ID that uniquely identifies this module among all modules within t ...
- xampp使用phpunit
1.将xampp/php的pear文件夹里面的phpunit文件夹复制到htdocs目录下 2.复制xampp/php的phpunit.bat到需要测试的目录 3.使用cmd命令切换至phpunit. ...
- sql操作之修改表结构
修改表的语法=========================增加列[add 列名]=========================①alter table 表名 add 列名 列类型 列参数[加的 ...
- delphi之动态库调用和串口通讯
串口通讯: Spcomm 控件属性: CommName :表示COM1,COM2等串口的名字: BaudRate:设定波特率9600,4800等 StartComm StopComm 函数Write ...
- 【转】Ubuntu下查看软件版本及安装位置
查看软件版本:aptitude show xxx 也可用apt-show-versions (要先安装sudo apt-get install apt-show-versions) 查看软件安装位置: ...
- lua module package.seeall选项
module 与 package.seeall http://blog.codingnow.com/2006/02/lua_51_module.html 使用 module("test&qu ...
- DuiLib学习笔记2——写一个简单的程序
我们要独立出来自己创建一个项目,在我们自己的项目上加皮肤这才是初衷.我的新建项目名为:duilibTest 在duilib根目录下面有个 Duilib入门文档.doc 我们就按这个教程开始入门 首先新 ...
- 隐藏,显示任务栏,桌面 C++代码
HWND desktop,task; desktop=FindWindow(L"ProgMan",NULL); task=FindWindow(L"Shell_TrayW ...