Minimum Inversion Number~hdu 1394
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.
InputThe 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.
OutputFor 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 这题先要补充一个逆序数的概念
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,
那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。
一个排列中所有逆序总数叫做这个排列的逆序数。也就是说,对于n个不同的元素,
先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规定从小到大为标准次序),
于是在这n个元素的任一排列中,当某两个元素的先后次序与标准次序不同时,
就说有1个逆序。一个排列中所有逆序总数叫做这个排列的逆序数。
(以上文字引用自百度百科)
由于数据较大求逆序数就必须用复杂度较少的方法求,强行暴力求逆序数表示应该没人会选择这样做吧。
线段树就是一个非常好的数据结构用于求逆序数。
题意:一个有N个数的序列中,每次把第一个放在序列的最后一个形成新的序列,
求这些序列中最小的逆序数是多少?
求出原始序列的逆序数S,将a[i]移到最后一位时,新的序列的逆序数为
原来序列的逆序值S+比a[i]大的数的个数(n-1-a[i])-a[i](比a[i]小的个数)
如果这题本菜鸟有什么地方理解不正确,请读者在下方给我留言,纠正我的错误。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 5010
int sum[maxn*+],a[maxn];
void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void build(int l,int r,int rt)
{
sum[rt]=;
if (l==r) return ;
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
}
void updata(int x,int l,int r,int rt )
{
if (l==r) {
sum[rt]++;
return ;
}
int m=(l+r)>>;
if (x<=m) updata(x,l,m,rt<<);
else updata(x,m+,r,rt<<|);
pushup(rt);
}
int query(int x,int y,int l,int r,int rt)
{
if (x<=l && r<=y) return sum[rt];
int m=(l+r)>>;
int ans=;
if (x<=m) ans+=query(x,y,l,m,rt<<);
if (y>m) ans+=query(x,y,m+,r,rt<<|);
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
build(,n-,);
int s=;
for (int i= ;i<n ;i++){
scanf("%d",&a[i]);
s+=query(a[i],n-,,n-,);
updata(a[i],,n-,);
}
int ans=s;
for (int i= ;i<n ;i++ ){
s+=(n-a[i]-)-a[i];
ans=min(ans,s);
}
printf("%d\n",ans);
}
return ;
}
Minimum Inversion Number~hdu 1394的更多相关文章
- 线段树 逆序对 Minimum Inversion Number HDU - 1394 Laptop
Minimum Inversion Number HDU - 1394 求最小反转数,就是求最少的逆序对. 逆序对怎么求,就是先把所有的数都初始化为0,然后按照顺序放入数字,放入数字前查询从这个数往后 ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 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 逆序数/树状数组
Minimum Inversion Number Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- hdu 1394 Minimum Inversion Number(逆序数对) : 树状数组 O(nlogn)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 //hdu 题目 Problem Description The inversion number ...
- HDU 1394——Minimum Inversion Number——————【线段树单点增减、区间求和】
Minimum Inversion Number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
随机推荐
- 洛谷 [P2661] 信息传递
求有向图的权值为一的最小环 并查集做法 维护一个dis[],表示i号元素到fa[i]的距离. 对于输入的每两个点u,v,询问这两个点的fa[]是否相同,如果相同就成环,维护最小值,mi=min(mi, ...
- BZOJ 3456: 城市规划 [多项式求逆元 组合数学 | 生成函数 多项式求ln]
3456: 城市规划 题意:n个点组成的无向连通图个数 以前做过,今天复习一下 令\(f[n]\)为n个点的无向连通图个数 n个点的完全图个数为\(2^{\binom{n}{2}}\) 和Bell数的 ...
- POJ 1755 Triathlon [半平面交 线性规划]
Triathlon Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6912 Accepted: 1790 Descrip ...
- 小甲鱼OD学习第8讲
这次我们的任务是破解这个有日期限制的软件 我们可以看到,这个一个有日期限制的软件,如图所示 首先,我们把程序载入OD,我们从字符串搜索入手,选择 所有参考文本字串,如图 我们输入相应的字符串尝试搜索 ...
- 启动mysql遇到1067问题
最近安装sql的时候,出现一些问题:启动的时候出现1067错误 在网上找了很多方法,比如删除mysql安装目录下date的bdata1, ib_logfile0, .. 等innodb的文件, 比如修 ...
- mac下更新自带的PHP版本到5.6
OS X 10.11自带的PHP版本是PHP 5.5.x,如果我们想更新PHP的版本到5.6或者是7.0该怎么办呢? 下载和安装PHP 5.6 打开终端并且运行如下命令: curl -s http:/ ...
- <script>标签中的 defer 与 async区别
在html里,使用<script>标签对脚本进行外部或内部引用,<script>标签包含了两个特殊的属性:defer与async,他们的区别如下: 1.若<script& ...
- 【Unity3D技术文档翻译】第1.0篇 AssetBundles
前言 "Unity圣典"是目前对官方文档翻译比较详细的,然而文档的最新更新日期是2013年,已经远远落后最新版本,参考意义有限.官方文档.脚本手册是学习Unity3D最直接有效的途 ...
- iOS程序闪退的原因以及处理办法
iOS程序闪退是一种比较常见的现象.闪退的情况很多,造成程序闪退的原因也很多. ================================启动时闪退======================= ...
- Mysql主从复制_模式之日志点复制
MySQL数据复制的原理 MySQL复制基于主服务器在二进制日志中跟踪所有对数据库的更改(更新.删除等等).因此,要进行复制,必须在主服务器上启用二进制日志. 每个从服务器从主服务器接收主服务器已经记 ...