HDU_1394_Minimum Inversion Number_线段树求逆序数
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16686 Accepted Submission(s): 10145
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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define maxn 5005
struct Node
{
int l,r;
int sum;
} tree[maxn<<]; void build(int l,int r,int rt)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].sum=;
if(tree[rt].l==tree[rt].r)
return;
int mid=(l+r)/;
build(lson);
build(rson);
} void update(int x,int l,int r,int rt)
{
if(tree[rt].l==x&&tree[rt].r==x)
{
tree[rt].sum++;
return;
}
int mid=(l+r)/;
if(x<=mid)
update(x,lson);
else
update(x,rson);
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
} int ans=;
int query(int L,int R,int l,int r,int rt)
{
if(L>R)
return ;
if(L==tree[rt].l&&R==tree[rt].r)
{
// cout<<tree[rt].sum<<endl;
return tree[rt].sum;
}
int mid=(l+r)/;
if(R<=mid)
return query(L,R,lson);
else if(L>mid)
return query(L,R,rson);
else
{
return query(L,mid,lson)+query(mid+,R,rson);
}
} int num[];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
ans=;build(,n,);
for(int i=; i<n; i++)
{
scanf("%d",&num[i]);
num[i]++;
}
//cout<<tree[16].l<<'*'<<tree[16].r<<endl;
for(int i=n-;i>=;i--)
{
//cout<<num[i]<<'&'<<endl;
if(i!=n-)
ans+=query(,num[i]-,,n,);
//cout<<ans<<endl;
update(num[i],,n,);
//cout<<ans<<endl;
}
int res=ans;
for(int i=;i<n-;i++)
{
res=res+n-num[i]-num[i]+;
ans=min(res,ans);
}
printf("%d\n",ans);
}
return ;
}
HDU_1394_Minimum Inversion Number_线段树求逆序数的更多相关文章
- [HDU] 1394 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 (Java/Ot ...
- <Sicily>Inversion Number(线段树求逆序数)
一.题目描述 There is a permutation P with n integers from 1 to n. You have to calculate its inversion num ...
- HDU-1394 Minimum Inversion Number(线段树求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- 线段树求逆序数方法 HDU1394&&POJ2299
为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...
- hdu 1394 (线段树求逆序数)
<题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...
- HDU - 1394 Minimum Inversion Number (线段树求逆序数)
Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs ( ...
- 【线段树求逆序数】【HDU1394】Minimum Inversion Number
题目大意: 随机给你全排列中的一个,但不断的把第一个数丢到最后去,重复N次,形成了N个排列,问你这N个排列中逆序数最小为多少 做法: 逆序数裸的是N^2 利用线段树可以降到NlogN 具体方法是插入一 ...
- hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)
题目传送门 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
随机推荐
- HashMap源码分析3:移除
本文源码基于JDK1.8.0_45. final Node<K,V> removeNode(int hash, Object key, Object value, boolean matc ...
- Ubuntu 16.04错误:The update information is outdated this may be caused by network...的问题解决
说明:这个问题没有最终的解决方案,只有不断的尝试. 错误: The update information is outdated this may be caused by network probl ...
- Cisco IOU Web Interface : Web IOU
https://github.com/dainok http://sns.clnchina.com.cn/space.php?uid=404779&do=blog&id=4298 ht ...
- Centos7: 设置UTC时区
timedatectl set-timezone UTC
- ZMQ源代码分析(一)-- 基础数据结构的实现
yqueue 和 ypipe zmq号称是"史上最快的消息队列",由此可见zmq中最重要的数据结构就是队列. zmq的队列主要由yqueue和ypipe实现.yqueue是队列的基 ...
- C++学习之多重继承与虚继承
一.多重继承 我们知道,在单继承中,派生类的对象中包含了基类部分 和 派生类自定义部分.同样的,在多重继承(multiple inheritance)关系中,派生类的对象包含了每个基类的子对象和自定义 ...
- Cocos Code IDE + Lua初次使用FastTiledMap的坑
近期想玩玩Lua.又想玩玩Cocos Code IDE.更加想写一个即时战斗的.防守的.会动的.有迷雾的.要探索的(旁白:给我停!)跑地图游戏. 于是我就用Cocos Code IDE来写游戏了.挑战 ...
- 完美解决EditText和ScrollView的滚动冲突(上)
在网上搜了一下EditText和ScrollView的滚动冲突,发现差点儿全部的解决方式都是触摸EditText的时候就将事件交由EditText处理,否则才将事件交由ScrollView处理.这样确 ...
- Ubuntu 13.10 安装 TeX Live 2013
注:笔者也是刚刚接触TeX系统,水平有限,若有疏漏之处还望指正. 中文解决方案 对于LaTeX中文排版,比较方便有这样的几种解决方案:LaTeX+CJK / LaTeX+XeTeX / CTeX.其中 ...
- BestCoder Round #61 (div.2) C.Subtrees dfs
Subtrees 问题描述 一棵有N个节点的完全二叉树,问有多少种子树所包含的节点数量不同. 输入描述 输入有多组数据,不超过1000组. 每组数据输入一行包含一个整数N.(1\leq N\leq ...