HDUOJ---1754 Minimum Inversion Number (单点更新之求逆序数)
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9342 Accepted Submission(s): 5739
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.
//线段树实现单点更新,并求和
#include<stdio.h>
#define maxn 5001
struct node{
int lef,rig,sum;
int mid(){ return lef+((rig-lef)>>) ;}
};
node seg[maxn<<];
int aa[maxn+];
void build(int left,int right,int p )
{
seg[p].lef=left;
seg[p].rig=right;
seg[p].sum=;
if(left==right) return ;
int mid=seg[p].mid();
build(left,mid,p<<);
build(mid+,right,p<<|);
}
void updata(int pos,int p,int val)
{
if(seg[p].lef==seg[p].rig)
{
seg[p].sum+=val;
return ;
}
int mid=seg[p].mid();
if(pos<=mid) updata(pos,p<<,val);
else updata(pos,p<<|,val);
seg[p].sum=seg[p<<].sum+seg[p<<|].sum;
}
int query(int be ,int en,int p)
{
if(be<=seg[p].lef&&seg[p].rig<=en)
return seg[p].sum;
int mid=seg[p].mid();
int res=;
if(be<=mid) res+=query(be ,en ,p<<);
if(mid<en) res+=query(be ,en ,p<<|);
return res;
}
int main()
{
int nn,i,ans;
while(scanf("%d",&nn)!=EOF)
{
ans=;
build(,nn-,);
for(i=;i<=nn;i++)
{
scanf("%d",&aa[i]);
updata(aa[i],,);
if(aa[i]!=nn-) ans+=query(aa[i]+,nn-,); //统计比其大的数
}
int min=ans;
for(i=;i<=nn;i++)
{
ans+=nn-*aa[i]-;
if(min>ans) min=ans;
}
printf("%d\n",min);
}
return ;
}
HDUOJ---1754 Minimum Inversion Number (单点更新之求逆序数)的更多相关文章
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU - 1394 Minimum Inversion Number (线段树求逆序数)
Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs ( ...
- HDU-1394 Minimum Inversion Number(线段树求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- Minimum Inversion Number(线段树求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)
题目传送门 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1394 Minimum Inversion Number 【线段树求逆序数】
之前写过树状数组的,再用线段树写一下--- #include<cstdio> #include<cstring> #include<iostream> #inclu ...
- HDU - 1394 Minimum Inversion Number(线段树求逆序数---点修改)
题意:给定一个序列,求分别将前m个数移到序列最后所得到的序列中,最小的逆序数. 分析:m范围为1~n,可得n个序列,求n个序列中最小的逆序数. 1.将序列从头到尾扫一遍,用query求每个数字之前有多 ...
- hdu1394--Minimum Inversion Number(线段树求逆序数,纯为练习)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- <Sicily>Inversion Number(线段树求逆序数)
一.题目描述 There is a permutation P with n integers from 1 to n. You have to calculate its inversion num ...
随机推荐
- jquery、js获取table,遍历输出tr中各个td的内容。
首先,依赖jquery.. $('#btntb').click(function(){ $('#tab tr').each(function(i){ // 遍历 tr $(this).children ...
- fisher精确检验(fisher’s exat test)和超几何分布
- 用java解析在OpenStreetMap上下载的地图数据
采用dom4j解析下载的xml文件,java程序如下: package gao.map.preprocess; import java.io.BufferedWriter; import java.i ...
- Data Binding MVVM 数据绑定 总结
示例代码:https://github.com/baiqiantao/DataBindingTest 参考:精通Android Data Binding Android Data Binding ...
- HTML-<td> 标签的 valign 属性
<html> <body> <table border="1" style="height:200px"> <tr&g ...
- javascript格式化json显示
// Example usage: http://jsfiddle.net/q2gnX/ var formatJson = function(json, options) { var reg = nu ...
- .NET破解之迅捷PDF转换器(续)
在以前的博文<.NET破解之迅捷PDF转换器>中使用了暴力破解的方法,现在软件版本从5.0升级到6.3,所以也尝试用新的方法. 方法一:暴力破解法 如往常一样,查找搜索到关键的函数,即Is ...
- GP开发示例:数据库去重
这个例子专业讲解基于ArcEngine使用GP开发的过程及遇到的问题.更多GP使用方法:GP使用心得 功能需求:现在外业第一次数据(简称调绘.mdb)和第二次数据(简称检查.mdb)有重复.第二次是在 ...
- Jquery Types 小结
JavaScript provides several built-in(内置的) datatypes. In addition to those, this page documents virtu ...
- AS3的反编译
一个as3写的swf反编译,修改类库: http://www.swfwire.com/ http://www.swfwire.com/decompiler 基于此类库的swf反编译工具(air) ht ...