Minimum Inversion Number(线段树求逆序数)
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15517 Accepted Submission(s): 9467
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
题解:线段树;也可以用归并排序,也可以用树状数组;注意每次把第一个放在最后这个条件;归并:http://www.cnblogs.com/handsomecui/p/4814442.html
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
const int MAXN=5010;
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define pushup tree[root]=tree[root<<1]+tree[root<<1|1]
#define mem(x,y) memset(x,y,sizeof(x))
int tree[MAXN<<2];
int a[MAXN];
int ans;
void update(int v,int root,int l,int r){
int mid=(l+r)>>1;
if(l==v&&r==v){
tree[root]++;
return;
}
if(mid>=v)update(v,lson);
if(mid<v)update(v,rson);
pushup;
}
void query(int L,int R,int root,int l,int r){
int mid=(l+r)>>1;
if(l>=L&&r<=R){
ans+=tree[root];
return;
}
if(mid>=L)query(L,R,lson);
if(mid<R)query(L,R,rson);
}
int main(){
int N;
while(~scanf("%d",&N)){
mem(tree,0);
ans=0;
int x;
for(int i=0;i<N;i++){
scanf("%d",&x);a[i]=x;
query(x+1,N-1,1,0,N-1);
// printf("%d\n",ans);
update(x,1,0,N-1);
}
int cnt=ans;
for(int i=0;i<N;i++){
cnt=cnt+N-1-a[i]-a[i];
ans=min(ans,cnt);
}
printf("%d\n",ans);
}
return 0;
}
Minimum Inversion Number(线段树求逆序数)的更多相关文章
- [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_线段树求逆序数
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- hdu 13394 Minimum Inversion Number 线段树
题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...
- 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 ...
- hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)
题目传送门 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 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 validationEngine 使用ajax验证不通过也提交表单
转自 http://mylfd.iteye.com/blog/2007227 validationEngine给我们为前端的表单验证减少了很大的工作量.大部分情况我们使用validationEngin ...
- Unity 4.2.0 官方最新破解版(Unity3D 最新破解版,3D游戏开发工具和游戏引擎套件)
Unity是一款跨平台的游戏开发工具,从一开始就被设计成易于使用的产品.作为一个完全集成的专业级应用,Unity还包含了价值数百万美元的功能强大的游戏引擎.Unity作为一个游戏开发工具,它的设计主旨 ...
- IM,游戏服务端 tcp 框架整理
Mina: Mina(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应 ...
- 07-2. A+B和C (15)
给定区间[-231, 231]内的3个整数A.B和C,请判断A+B是否大于C. 输入格式: 输入第1行给出正整数T(<=10),是测试用例的个数.随后给出T组测试用例,每组占一行,顺序给出A.B ...
- C++面试经常涉及的概念1
1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new对应.free只会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,ne ...
- Zombie.js Insanely fast, headless full-stack testing using Node.js
(92) Is there a better crawler than Scrapy? - Quora Is there a better crawler than Scrapy?Edit Insan ...
- C# ADO基础(使用using操作数据库)
1.使用using 来对数据库进行操作,using是资源释放的一种缩写,用于实现了实现了IDisposable接口(释放对象资源的接口是IDisposable) private void button ...
- Ubuntu下远程访问MySQL数据库
MySQL远程访问的命令 格式: mysql -h主机地址 -u用户名 -p用户密码 jack@jack:~$ mysql -h192.168.5.154 -usaledata -pEnter pas ...
- 【c语言】推断一个数是不是2的n次方
// 推断一个数是不是2的n次方 #include <stdio.h> void judge_n(int a) { int b = a - 1; if ((a & b) == 0) ...
- Android Fragment 嵌套使用报错
在新的SDK每次创建activity时,会自己主动生成 <pre name="code" class="java">public static c ...