HDU 1394Minimum Inversion Number 数状数组 逆序对数量和
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18543 Accepted Submission(s): 11246
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
题意:求出对(ai,aj)(i<j,ai>aj)的全部数量。即求ai右边比ai小的数的个数和。每次变换a的序列,求出最小的逆序对数量和。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=1e6+,INF=1e9+;
int a[MAXN],c[MAXN],ans[MAXN];
int lowbit(int x)
{
return x&(-x);
}
void add(int i,int val)
{
for(i; i<=MAXN; i+=lowbit(i))
c[i]+=val;
}
int sum(int i)
{
int s=;
for(i; i>; i-=lowbit(i))
s+=c[i];
return s;
}
int main()
{
int i,j,t,n;
while(scanf("%d",&n)!=EOF)
{
for(i=n; i>=; i--)
scanf("%d",&a[i]);
memset(c,,sizeof(c));
memset(ans,,sizeof(ans));
int cou=;
for(i=; i<=n; i++)
{
ans[i]=sum(a[i]+);
cou+=ans[i];
add(a[i]+,);
}
int Min=cou;
for(i=n; i>; i--)
{
for(j=; j<=n; j++)
{
if(j==i) continue;
if(a[i]<a[j])
{
cou++;
ans[j]++;
}
}
cou-=ans[i];
ans[i]=;
if(cou<Min) Min=cou;
}
cout<<Min<<endl;
}
return ;
}
逆序对
HDU 1394Minimum Inversion Number 数状数组 逆序对数量和的更多相关文章
- [树状数组+逆序对][NOIP2013]火柴排队
火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...
- 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 (树状数组 && 规律 && 逆序数)
题意 : 有一个n个数的数列且元素都是0~n-1,问你将数列的其中某一个数及其前面的数全部置到后面这种操作中(比如3 2 1 0中选择第二个数倒置就产生1 0 3 2)能产生的最少的逆序数对是多少? ...
- hdu 5497 Inversion 树状数组 逆序对,单点修改
Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...
- hdu 1394 Minimum Inversion Number - 树状数组
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- [hdu1394]Minimum Inversion Number(树状数组)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU 1394Minimum Inversion Number
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- HDU 2689Sort it 树状数组 逆序对
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
随机推荐
- c#上iOS apns p12文件制作记录 iOS推送证书制件
前期一些准备工作可参考:http://jingyan.baidu.com/article/7082dc1c6bb86de40a89bd1a.html 1.在桌面上建一个"apns_p12&q ...
- mysql 的 infobright 数据库的 mediumblob 显示不了数据
需要修改mysql的配置文件: /var/www/html/phpmyadmin/config.inc.php 增加: $cfg['ProtectBinary'] = FALSE; 即可
- 黄聪:PHP7.0中htmlspecialchars出错解决方案(wordpress)
htmlspecialchars( $context, ENT_COMPAT, 'UTF-8' )
- [转]理解android.intent.category.LAUNCHER 具体作用
转自:http://blog.csdn.net/jackrex/article/details/9189657 android.intent.category.LAUNCHER 具体有什么作用?我做一 ...
- (C#) Tasks 中的异常处理(Exception Handling.)
多线程编程中要注意对线程异常的处理.首先写个例子. 一个线程用于显示信息(Show Messages).主线程用于做其他工作(Do Works). using (Task taskShowMessag ...
- sql server 相似度对比
转自:http://www.dotblogs.com.tw/rachen/archive/2008/10/07/5611.aspx 函數一.產生 Like 比對用字串 ) ) ) as begin / ...
- gerrit 修改前一次提交的方法(转载)
From:http://sinojelly.sinaapp.com/2011/08/git-changes-submitted-by-the-previous-method-pay-special-a ...
- (转)pdf文件结构
PDF文件结构(一) ————物理结构 PDF(Portable Document Format,便携式文档结构)是一种很有用的文件格式,其最大的特点是平台无关而且功能强大(支持文字/图象/ ...
- 清理Oracle安装目录里的一些日志信息
最近测试环境服务器上磁盘空间紧张,表空间里面的数据也不知道开发哪些需要哪些不需要,而且因为此测试库运行时间比较久远,起码有三年了.于是就从清理Oracle安装目录下的日志下手. 发现在一般这几个日志, ...
- [转载]:fortran之format格式化输出总结
先贴一段别人总结好的: 格式化输出的控制字符非常的丰富,但常用的并不多,一般说来:" I .F.E.A.X "是最常使用的几个格式,最 好把它们都记下来. Iw[.m] 以w个字符 ...