HDU 1394Minimum Inversion Number
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种序列中倒置数的最小个数,即满足下标i<j,a[i]>a[j]。
其实说白了就是求不停倒置的最小逆序数
求逆序数用线段树或者树状数组都是挺方便的,每次加一个数进去,看此时后面的数有几个。
但这样肯定会超时,我们可以首先写出一开始的逆序数,发现每次倒置后,逆序数为减少a[i],而增加n-1-a[i]的。减少比它小的,增加比它大的
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int a[maxn<<];
int add[maxn];
#define lson l,m,k<<1
#define rson m+1,r,k<<1|1
void hehe(int k)
{
a[k]=a[k<<]+a[k<<|];
}
void build(int l,int r,int k)
{
a[k]=;//先是假设这棵树都为0
if(l==r) return ;
int m=(l+r)>>;
build(lson);
build(rson);
}
int query(int x,int y,int l,int r,int k)
{
if(x<=l&&r<=y)
return a[k]; int m=(l+r)>>;
int s=;
if(x<=m) s+=query(x,y,lson);
if(y>m) s+=query(x,y,rson);
return s;
}
void update(int p,int l,int r,int k)
{
if(l==r)
{
a[k]++;//增加个数
return;
}
int m=(l+r)>>;
if(p<=m) update(p,lson);
else update(p,rson);
hehe(k);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
build(,n-,);
int sum=;//为一开始的逆序数
for(int i=;i<n;i++)
{
scanf("%d",&add[i]);
sum+=query(add[i],n-,,n-,);
update(add[i],,n-,);
}
int minn=sum;
for(int i=;i<n;i++)
{
sum+=n-add[i]-add[i]-;
minn=min(minn,sum);
}
cout<<minn<<endl;
}
return ;
}
HDU 1394Minimum Inversion Number的更多相关文章
- HDU 1394Minimum Inversion Number 数状数组 逆序对数量和
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU 1394Minimum Inversion Number(线段树)
题目大意是说给你一个数组(N个),没戏可以将其首部的k(k<N)个元素移动至尾部,这样总共会形成N个序列 现在要求这n个序列中逆序对数最少的那一个序列有多少个逆序对 最初的确是没太多思路,就算知 ...
- HDU 1394-Minimum Inversion Number(BIT)
题意: 给你n个数字的序列 每次把第一个数字放到最后 得到一个新序列 一共有n个序列求这些序列中哪个序列含最小的总的逆序数 (输出最小总逆序数) 分析: 用BIT求出初始各数的逆序数,第一个数放最后它 ...
- 1394-Minimum Inversion Number
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- hdu Minimum Inversion Number(逆序数的小知识与线段树)
飞! 题解 首先,求逆序数对的思路: 1.得到整个数列后,从前往后扫,统计比a[i]小的,在a[i]后面的有多少个 这样做的话,应该是只有n2的暴力作法,没想到更好的方法 2.统计a[i]前面的,且比 ...
- 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 ...
随机推荐
- JS n秒后自动跳转实例
<p><a href="<?php echo base_url();?>usercenter/index" id="message" ...
- PHP的抽象类和抽象方法以及接口总结
PHP中的抽象类和抽象方法自己用的不多,但是经常会在项目中看到别人使用,同样,今天在看别人的代码的时候,发现使用了抽象类,就总结下: 抽象类:1.如果一个类中有一个方法是抽象方法,则这个类就是抽象类: ...
- Spring Cloud 微服务入门(二)--Spring Cloud 架构
Spring Cloud整体核心架构:Rest服务,在Spring Cloud配置过程中,都是遵循Rest风格规范,在Rest处理中,必不可少两个对象端:服务的提供者(provider)和服务消费者( ...
- Enable directory listing on Nginx Web Server
1:Test environment [root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core ...
- IoC和AOP扩展
一.构造注入 二.使用p命名空间注入属性值 三.注入不同数据类型 <?xml version="1.0" encoding="UTF-8"?> &l ...
- 使用Letsencrypt做SSL certificate
为什么要使用Letsencrypt做SSL certificate? 最简单直接的原因是免费.但是免费存在是否靠谱的问题,尤其是对安全要求比较高的网站,需要考虑使用letsencrypt的安全性是否符 ...
- memcache类的扩展函数
Memcache — Memcache类 /****连接****/1.Memcache::connect – 创建一个Memcache对象语法:bool Memcache::connect ( str ...
- day 19 反射
1.isinstance, type, issubclass 的含义 isinstance: 判断你给对象时候是xxx类型的.(向上判断) type: 返回xxx对象的数据类型 issubclass ...
- Java线程状态图
嘤,先盗图一张,后面再补充描述!
- IAP笔记
1) 首先是IAP BootLoader程序设置:根据common.h里面的宏定义,设置BootLoader所占用的Flash空间. 2) 设置IAP UserApp程序设置:该型号Flash ...