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 题意: 给一个序列由 ...
随机推荐
- Markdown 安装图解(破解汉化教程)
http://blog.csdn.net/taokai_110/article/details/72934818 终于解决了问题
- cocos2d-x代码阅读笔记 - 入口
每一个C\C++程序都有一个非常有名的入口函数 main(),在Windows系统下,这个函数就变成了WinMain函数. 在cocos2d-x 2.0.4的Windows版本中,main函数非常简单 ...
- Python中Template使用的一个小技巧
Python中Template是string中的一个类,可以将字符串的格式固定下来,重复利用. from string import Template s = Template("there ...
- 浅谈JavaScript的事件(事件模拟)
事件经常由操作或者通过浏览器功能触发,通过JavaScript也可以触发元素的事件.通过JavaScript触发事件,也称为事件的模拟. DOM中事件模拟 可以document的createEvent ...
- [Python Study Notes]CS架构远程访问获取信息--Client端v2.0
更新内容: 1.增加内存信息获取 2.增加电池信息获取 3.增加磁盘信息获取 4.重新布局窗体 5.增加窗体名称 6.增加连接成功之前,不可按压 效果图: '''''''''''''''''''''' ...
- Appium基于Python unittest自动化测试 & 自动化测试框架 -- PO并生成html测试报告
基于python单元测试框架unittest完成appium自动化测试,生成基于html可视化测试报告 代码示例: #利用unittest并生成测试报告 class Appium_test(unitt ...
- echarts legend 重叠 (转载)
解决方案: 1. 调整option中的grid.top值才能避免重叠:(可以设置定制,也可以定义了一个计算公式) 2. 文档注明[特殊字符串 ''(空字符串)或者 '\n' (换行字符串)用于图例的 ...
- Angular:利用内容投射向组件输入ngForOf模板
现在,我们写一个组件puppiesListCmp,用于显示小狗狗的列表: //puppies-list.component.ts @Component({ selector: 'puppies-lis ...
- 如何遍历 Windows 摄像头设备?
#include <stdlib.h> #include <iostream> #include <Windows.h> #include <comdef.h ...
- SpringBoot+gradle+idea实现热部署和热加载
前言 因为之前使用myeclipes的同学就知道,在使用myeclipes的时候,java文件或者jsp文件写完之后会被直接热加载到部署的容器中,从而在开发的时候,不同经常去重启项目,从而达到了增加开 ...