hdu 1394 Minimum Inversion Number(线段树之 单点更新求逆序数)
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit:
65536/32768 K (Java/Others)
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.
10
1 3 6 9 0 8 5 7 4 2
16
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N = 5005;
#define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
int sum[N<<2], a[N]; void Push_Up(int root)
{
sum[root] = sum[root<<1] + sum[root<<1|1];
}
void build_tree(int l, int r, int root)
{
sum[root] = 0;
if(l == r) return;
int mid = (l + r) >> 1;
build_tree(lson);
build_tree(rson);
}
void update(int p, int l, int r, int root)
{
if(l == r)
{
sum[root]++;
return;
}
int mid = (l + r) >> 1;
if(p <= mid) update(p, lson);
else update(p, rson);
Push_Up(root);
}
int Query(int L, int R, int l, int r, int root)
{
if(L <= l && r <= R)
{
return sum[root];
}
int mid = (l + r) >> 1;
int ans = 0;
if(L <= mid) ans += Query(L, R, lson);
if(R > mid) ans += Query(L, R, rson);
return ans;
} int main()
{
int n, i;
while(~scanf("%d",&n))
{
build_tree(0, n-1, 1);
int res = 0;
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
res += Query(a[i], n-1, 0, n-1, 1); //查询已经插入到线段树中的数有多少个数比a[i]大
update(a[i], 0, n-1, 1); //把这个数插入到线段树中
}
int ans = res;
for(i = 0; i < n; i++)
{
res += (n - a[i] - 1) - a[i];
ans = min(ans, res);
}
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 线段树
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...
- 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 (树状数组)
题目链接 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的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...
随机推荐
- sofa-pbrpc 1.1.1 发布,RPC 网络通信库
https://www.oschina.net/news/77372/sofa-pbrpc-1-1-1 https://www.oschina.net/p/sofa-pbrpc
- pathload --有效的网络带宽估计方法
上一篇博客简述了现行的带宽估计的方法,分类,以及一些问题. 见:http://blog.csdn.net/ice110956/article/details/11071969 上文列出了13种现行的方 ...
- leetcode 最长连续序列 longest consecutive sequence
转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...
- java jquery 函数多參数传递
业务需求: 名次 伙伴 业绩 签单 面谈 每日目标 1 文彬 5100 6 10 查看目标 2 马红月 4550 4 6 查看目标 3 王刚 4100 3 9 查看目标 4 郭亚凯 3450 4 ...
- maven的命令使用笔记
(1)创建web项目mvn archetype:create -DgroupId=com.mycompany.webapp -DartifactId=myweb -DarchetypeArtif ...
- [置顶] 初识window.location.search
window.location.search是从当前URL的?号开始的字符串 如:http://www.domain.com/item?id=0064014 它的search就是?id=0064014
- ubuntu12.04安装搜狗输入法配置,安装packettracer字体设置,软件推荐
装上系统,后的各种配置,各种出错之后的解决. 一.安装搜狗输入法 如果系统以前安装了先卸载: sudo apt-get remove fcitx* #删除配置文件 sudo apt-get purge ...
- 深度学习系列之CNN核心内容
导读 怎么样来理解近期异常火热的深度学习网络?深度学习有什么亮点呢?答案事实上非常简答.今年十月份有幸參加了深圳高交会的中科院院士论坛.IEEE fellow汤晓欧做了一场精彩的报告,这个问题被汤大神 ...
- 创建android画笔程序的样例(有镜面效果)
先上图: 关键是在检測到手指移动的时候用mPath.quadTo的方法,android sdk解释是: Add a quadratic bezier from the last point, appr ...
- leetcode解析回文子串拆分
转载请注明来自souldak,微博:@evagle Given a string s, partition s such that every substring of the partition i ...