HDU 1394:Minimum Inversion Number(线段树区间求和单点更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1394
Minimum 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.
1 3 6 9 0 8 5 7 4 2
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 5005
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
struct node
{
int l,r,value;
}tree[N<<];
int a[N];
/*
求移位后的最小逆序数
Update单点增减
Query区间求和
*/
void Build(int rt,int l,int r)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].value=;
if(l==r) return ;
int m=(l+r)>>;
Build(rt<<,l,m);
Build(rt<<|,m+,r);
} void Update(int rt,int num)
{
if(tree[rt].l==num&&tree[rt].r==num){
tree[rt].value=;
return ;
}
int m=(tree[rt].l+tree[rt].r)>>;
if(num<=m) Update(rt<<,num);
else Update(rt<<|,num);
tree[rt].value=tree[rt<<].value+tree[rt<<|].value;
} int Query(int rt,int l,int r)
{
/*
画个图就明白了,搜寻的区间为[l,r],m代表当前节点的左儿子和右儿子的分支
如果左儿子有包含该区间的元素,即l<=m,那么就要继续递归搜寻左儿子
如果右儿子有包含该区间的元素,即m<r,那么就要继续递归搜寻右儿子
当当前的节点的区间被搜寻的区间[l,r]覆盖的话,就说明该段区间完全在[l,r]里面
那么返回该节点的值
*/
if(l<=tree[rt].l&&tree[rt].r<=r){
return tree[rt].value;
}
else{
int m=(tree[rt].l+tree[rt].r)>>;
int s1=,s2=;
if(l<=m) s1=Query(rt<<,l,r);
if(r>m) s2=Query(rt<<|,l,r);
return s1+s2;
}
} int main()
{
int n;
while(~scanf("%d",&n)){
Build(,,n);
int sum=;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
a[i]++;
sum+=Query(,a[i]+,n);
Update(,a[i]);
}
int res = sum;
for(int i=n-;i>=;i--){
sum=sum-(n-a[i])+(a[i]-);
if(sum<res) res=sum;
}
printf("%d\n",res);
}
return ;
}
HDU 1394:Minimum Inversion Number(线段树区间求和单点更新)的更多相关文章
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- hdu - 1394 Minimum Inversion Number(线段树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...
- [HDU] 1394 Minimum Inversion Number [线段树求逆序数]
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- hdu 1116 敌兵布阵 线段树 区间求和 单点更新
线段树的基本知识可以先google一下,不是很难理解 线段树功能:update:单点增减 query:区间求和 #include <bits/stdc++.h> #define lson ...
- HDU 1394 Minimum Inversion Number(线段树 或 树状数组)
题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...
- HDU 1394 Minimum Inversion Number 线段树
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...
- HDU 1394 Minimum Inversion Number (树状数组)
题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...
- 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: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- 图形化界面安装oracle报错Could not execute auto check for display colors using command /usr/bin/xdpyinfo. Check if the DISPLAY variable is set.
问题描述: 在Linux + oracle 安装时,采有root 帐号登录x-windows 界面,然后 $su oracle 登录录安装Oracle 报以下错误: >>> Coul ...
- SQL Server查询当前连接数
行数就是连接数,每一行是连接详情 SELECT * FROM [Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN ( SELECT [DBID] FROM [M ...
- js 生成表格及其颜色
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- JS 三个对话框
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.
Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when ...
- Keil5生成bin文件
进入“Options for Target”设置界面如下: 如图所示方框中输入: fromelf.exe --bin -o "$L@L.bin" "#L" 生成 ...
- JS实时检测文本框内容长度
通过js代码实时监测,文本框内容的变化以及长度,下图是一个实际使用场景. HTML部分: <input id="Text1" type="text" on ...
- Win8 Metro(C#)数字图像处理--2.71Sigma平滑滤波器
原文:Win8 Metro(C#)数字图像处理--2.71Sigma平滑滤波器 [算法说明] Sigma平滑滤波器是构造一个模板,比如3*3大小的模板,计算这个模板对应的像素的标准差d,然后 ...
- textblock的LineHeight的调整
原文:textblock的LineHeight的调整 <TextBlock Width="113.594" Height="73.667" Text=&q ...
- js操作select控件大全(包含新增、修改、删除、选中、清空、判断存在等)
原文:js操作select控件大全(包含新增.修改.删除.选中.清空.判断存在等) js操作select控件大全(包含新增.修改.删除.选中.清空.判断存在等) js 代码// 1.判断select选 ...