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 题意: 给一个序列由 ...
随机推荐
- BZOJ 4195: [Noi2015]程序自动分析 [并查集 离散化 | 种类并查集WA]
题意: 给出若干相等和不等关系,判断是否可行 woc NOI考这么傻逼的题飞快打了一个种类并查集交上了然后爆零... 发现相等和不等看错了异或一下再叫woc90分 然后发现md$a \neq b, a ...
- 安装RabbitMQ(一)
RabbitMQ简介 RabbitMQ 是由 LShift 提供的一个 Advanced Message Queuing Protocol (AMQP) 的开源实现,由以高性能.健壮以及可伸缩性出名的 ...
- centos7安装部署gitlab服务器
[gitlab需要内存至少4GB] 我这里使用的是centos 7 64bit,我试过centos 6也是可以的! 1. 安装依赖软件 yum -y install policycoreutils ...
- 设置修改CentOS系统时区
一.时区 1. 查看当前时区date -R 2. 修改设置时区方法(1)tzselect方法(2) 仅限于RedHat Linux 和 CentOS系统timeconfig方法(3) 适用于Debia ...
- 使用单元素的枚举类型实现Singleton
从java1.5版本开始,实现singleton出现了第三种方式: public enum SingleTon { INSTANCE; public void speak() { System.out ...
- python爬虫提取冰与火之歌五季的种子
# -*- encoding:utf-8 -*- import requests import re import sys reload(sys) sys.setdefaultencoding(&qu ...
- CODE大全浅谈谷歌adsense与PIN码
我的博客:CODE大全:www.codedq.net:业余草:www.xttblog.com:爱分享:www.ndislwf.com或ifxvn.com. 近期由于校园招聘笔试和面试等诸多忙碌时间,博 ...
- PAT乙级1065 map
思路:检查某个客人是否有伴侣,如果有,伴侣是否也出现即可. 注意:0个单身狗的时候,不要输出多余的'\n', 否则会出现格式错误. AC代码 #include <stdio.h> #inc ...
- Spring 中@NotNull, @NotEmpty和@NotBlank之间的区别是什么?
简述三者区别 @NotNull://CharSequence, Collection, Map 和 Array 对象不能是 null, 但可以是空集(size = 0). @NotEmpty://Ch ...
- Ansible自动化运维笔记1(安装配置)
1.Ansible的安装 pip install ansible==1.9.1 ansible1.9.1版本依赖的软件有 Python2.6以上版本 paramiko模块 PyYAML Jinja2 ...