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. JVM性能监控与故障处理命令行工具

    JDK命令行工具 Sun公司作为”礼物“赠送给JDK使用者的工具: 这些命令行工具大多是jdk/lib/tools.jar类库的一层薄包装,主要功能代码是在tools类库(不属于java的标准API) ...

  2. ArcGIS Server GP服务发布与测试(基础版)

    版本:ArcGIS Server 10.4    环境:win10 测试目标:创建一个GP服务,功能为根据要素属性信息(如FID)选择出输入内容的对应匹配要素. 注:这个测试非常基础,仅供新手发布GP ...

  3. JavaScript 字符 &quot;转换

    后台把一个Json类型的数据当成字符串返回到前台,但是到前台变成了下面的这个样子 "[{"name":"IE","y":72},{ ...

  4. php命名、注释规范

    一.注释 1.文件头部模板 /** *这是一个什么文件 * *此文件程序用来做什么的(详细说明,可选.). * @author richard<e421083458@163.com> * ...

  5. 《奥威Power-BI智能分析报告制作方法 》精彩回顾

     上次课我们简单介绍了奥威Power-BI的智能分析报告,并展示了报告与图表相结合的应用场景.图文分析报表的意义不只在于美观,更重要的是固定框架下的灵活性和追根究底的动态分析,有着很强的实用性.上节课 ...

  6. 去除行内(inline/inline-block)元素之间的间距

    先展示一下,行内元素之间存在间距,实例代码如下: <style> div { color: #fff; padding: 25px 50px; } .inline-f00 { displa ...

  7. NSFileManager 遍历目录

    NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *documentPath = [NSHomeDirecto ...

  8. Java中String类的方法及说明

    String : 字符串类型 一.      String sc_sub = new String(c,3,2);    //      String sb_copy = new String(sb) ...

  9. windows7 professional.iso

    目前使用的笔记本是PC,在重新安装系统的时候,我比较挑剔. 我希望它是原样的,我希望它能够 windows update,拥有 office 2007/2003,它的个人文件夹就原封不动地在默认在 C ...

  10. Web 通信 之 长连接、长轮询(转)

    Web 通信 之 长连接.长轮询(long polling) 基于HTTP的长连接,是一种通过长轮询方式实现"服务器推"的技术,它弥补了HTTP简单的请求应答模式的不足,极大地增强 ...