HDU 1394——Minimum Inversion Number——————【线段树单点增减、区间求和】
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
System Crawler (2015-03-30)
Description
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.
Input
Output
Sample Input
1 3 6 9 0 8 5 7 4 2
Sample Output
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn=5500;
int num[maxn*4];
void PushUP(int rt){ num[rt]=num[rt*2]+num[rt*2+1];
}
void build(int rt,int L,int R){ num[rt]=0;
if(L==R)
return ;
build(lson);
build(rson);
}
int query(int rt,int L,int R,int l_ran,int r_ran){ if(l_ran<=L&&R<=r_ran){ return num[rt];
}
int ret=0;
if(l_ran<=mid){ ret+=query(lson,l_ran,r_ran);
}
if(r_ran>mid){ ret+=query(rson,l_ran,r_ran);
}
return ret;
}
void update(int rt,int L,int R,int pos){ if(L==R){ num[rt]++;
return ;
}
if(pos<=mid){ update(lson,pos);
}
if(pos>mid){ update(rson,pos);
}
PushUP(rt);
}
int main(){ int n;
while(scanf("%d",&n)!=EOF){ build(1,0,n-1);
int sum=0;
int a[5050];
for(int i=0;i<n;i++){ scanf("%d",&a[i]);
int tmp=query(1,0,n-1,a[i],n-1);
sum+=tmp;
update(1,0,n-1,a[i]);
}
int ans=sum;
for(int i=0;i<n;i++){ sum=sum+n-2*a[i]-1;
if(ans>sum){
ans=sum;
}
}
printf("%d\n",ans);
}
return 0;
}
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 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 ...
- hdu 1394 Minimum Inversion Number(树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...
- hdu 13394 Minimum Inversion Number 线段树
题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...
随机推荐
- ASP.NET MVC 开源建站系统 ZKEACMS 推荐,从此网站“拼”起来
一个挺有意思的项目,跟拼图一样的创建网站,先来几张GIF感受一下: 官方地址:http://www.zkea.net/zkeacms 下载地址:https://github.com/SeriaWei/ ...
- DES加密与解密MD5加密帮助类
public class TrialHelper { //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xA ...
- WinForm中DataGridView的使用(五) - 自定义列
DataGridView支持指定DataGridViewImageColumn.DataGridViewButtonColumn等特殊类型的列,加入到Columns中. DataGridViewIma ...
- SQLServer 统计查询语句消耗时间
--方法1[set statistic ]: set statistics time on go --执行语句 xxxx go set statistics time off --方法2[getDat ...
- jstack应用-查找CPU飚高的原因
场景 在系统上线后,经常会遇到运维的同学跑过来说:“这次发版后,cpu线程使用率到一场,到100%了”.这时候不要慌,可以使用堆转储来分析到底是哪个线程引起的. 查找元凶 发现pid=17850的进程 ...
- windows server2008虚拟机系统盘扩容
windows server2008虚拟机的系统盘空间过小,对系统盘进行扩容,操作如下: 1.将虚拟机关机: 2.VMware对该虚拟机进行设置,选中磁盘,点击扩容,输入扩容大小,等待扩容完成: 3. ...
- ubuntu 16.4安装toolsbelt heroku
https://devcenter.heroku.com/articles/getting-started-with-python#set-up # Run this from your termin ...
- python 时间相关函数
python 中与时间处理相关的模块包括 time.datetime.以及 calendar time 模块 time() 函数:time() 函数用于返回当前时间的时间戳(1970年01月08时00 ...
- 特殊用途语言特性(默认实参/内联函数/constexpr函数/assert预处理宏/NDEBUG预处理变量)
默认实参: 某些函数有这样一种形参,在函数的很多次调用中它们都被赋予一个相同的值,此时,我们把这个反复出现的值称为函数的默认实参.调用含有默认实参的函数时,可以包含该实参,也可以省略该实参. 需要特别 ...
- ant实例
<?xml version="1.0" encoding="UTF-8" ?> <project name="javaTest&qu ...