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的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...
随机推荐
- 关于jquery文件上传插件 uploadify 3.1的使用
要使用uplaodify3.1,自然要下载相应的包,下载地址http://www.uploadify.com/download/,这里有两种包,一个是基于flash,免费的,一个是基于html5,需要 ...
- 多字符集(ANSI)和UNICODE及字符串处理方式准则
在我们编写程序的时候,使用最多的是字符串的处理,而ANSI和UNICODE的相互转换经常搞的我们头晕眼乱. 应该说UNICODE是一种比较好的编码方式,在我们的程序中应该尽量使用UNICODE编码方式 ...
- thinkphp模版调用函数方法
原文:thinkphp模版调用函数方法 {变量|函数1|函数2|函数3=参数1,参数2,参数3,###} ###为第4个参数,代表变量替换为第4个参数 举例: {$username|substr=0, ...
- Java方法区和运行时常量池溢出问题分析(转)
运行时常量池是方法区的一部分,方法区用于存放Class的相关信息,如类名.访问修饰符.常量池.字段描述.方法描述等. String.intern()是一个native方法,它的作用是:如果字符串常量池 ...
- Open Source RTOS
http://www.osrtos.com/ Name License Platforms Description Last updated FreeRTOS Modified GPL MSP ...
- Referer反反盗链
0x00 前言 最近用Python非常多,确实感受到了Python的强大与便利.但同时我并没有相见恨晚的感觉,相反我很庆幸自己没有太早接触到Python,而是基本按着C→C++→Java→Python ...
- hdu 4715
#include<stdio.h> #include<string.h> int prime[1100000],p[1000000],ans; void pri() { ...
- [置顶] Cocos2d-x 实例源码分析之二 小实例的主框架
这篇文章是分析第一个小实例ActionTest的源码.其实所有实例程序的结构都是一样的,只有特定方法里的代码不同,大的框架都是一样的.也就是说看完这篇文章你就可以自己开始分析其他源码了. 废话不多说, ...
- Android 进行单元測试难在哪-part3
原文链接 : HOW TO MAKE OUR ANDROID APPS UNIT TESTABLE (PT. 1) 原文作者 : Matthew Dupree 译文出自 : 开发技术前线 www.de ...
- lua的table库中经常使用的函数
lua提供了一些辅助函数来操作table. 比如,从list中insert和remove元素,对array的元素进行sort.或者concatenate数组中的全部strings.以下就具体地解说这些 ...