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. linux下缓存的查看/修改

    起因: 安装openstack过程中内存不够大,提高内存后想起缓存一般设置为内存的两倍. 缓存的实质是硬盘开辟一个空间,然后设置这个空间为缓存. 查看缓存大小 free -m free -m tota ...

  2. 为什么Visual Studio的安装目录下有名为1033或2052的文件夹?

    一直不清楚MS诸多产品的安装目录下为什么总有个名为1033或者2052的目录 搜索一下才知道,原来叫 LCID(Locale ID,区域性标识符) 常见的如:1033表示英语(美国),2052表示中文 ...

  3. TestNG的一个不足之处

    PS:本博客selenium分类不会记载selenium打开浏览器,定位元素,操作页面元素,切换到iframe,处理alter.confirm和prompt对话框这些在网上随处可见的信息:本博客此分类 ...

  4. github 或者gitlab 设置添加SSH, 避免每次提交重复输入用户名

    克隆项目二种方式: 1. 使用https url克隆,   复制https url 然后到 git clone https-url 2.使用 SSH url 克隆却需要在克隆之前先配置和添加好 SSH ...

  5. CentOS 配置网络yum源

    配置网络yum源(需要保证外网开通,使用网易163提供开源镜像站) 1.下载repo文件 方法一:下载到本地windows再上传到linux服务器上: 下载地址 : http://mirrors.16 ...

  6. html5 canvas围绕中心点旋转

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 【转】ubuntu 重启命令,ubuntu 重启网卡方法

    ubuntu 重启命令 重启命令:1.reboot2.shutdown -r now 立刻重启(root用户使用)3.shutdown -r 10 过10分钟自动重启(root用户使用)4.shutd ...

  8. 使用inherit属性值继承其父元素样式来覆盖UA自带样式。

    像button.input这样的表单控件,不同的浏览器都会有自己的样式风格(UA样式).我们可以使用inherit继承其父元素样式,从而覆盖浏览器的UA样式. button, input, selec ...

  9. PHP js使用ajax异步处理方式请求PHP,解决数组中文乱码

    html端: <html> <head> <script type="text/javascript" src="jquery/jquery ...

  10. Coursera台大机器学习基础课程学习笔记2 -- 机器学习的分类

    总体思路: 各种类型的机器学习分类 按照输出空间类型分Y 按照数据标记类型分yn 按照不同目标函数类型分f 按照不同的输入空间类型分X 按照输出空间类型Y,可以分为二元分类,多元分类,回归分析以及结构 ...