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 ...
随机推荐
- new出来的对象无法调用@Autowired注入的Spring Bean
@Autowired注入Spring Bean,则当前类必须也是Spring Bean才能调用它,不能用new xxx()来获得对象,这种方式获得的对象无法调用@Autowired注入的Bean. 1 ...
- 日常工作中常见的mysql优化技巧
1.介绍一下MYSQL经常使用的优化技巧. MySQL 自带 slow log 的分析工具 mysqldumpslow ,可是没有说明.本文通过分析该脚本,介绍了其用法. slow log 是 MyS ...
- Codeforces Round #250 (Div. 2)B. The Child and Set 暴力
B. The Child and Set At the children's day, the child came to Picks's house, and messed his house ...
- python 文档生成 字符串替换
替换 fnr, fnr_source, fnw = 'my.py.html', '产品清单.txt', 'my.py.res.html'd_source = {}with open(fnr_sourc ...
- java分页之页面分页
import java.util.ArrayList; import java.util.List; /** * * @author cheney * * @date Aug 31, 2012 */ ...
- android note【转】
本文转载自:http://blog.csdn.net/u012719256/article/details/52094982 1.重要的property属性 #define ANDROID_RB_PR ...
- strok函数用法【转】
本文转载自:http://blog.csdn.net/hexiechina2010/article/details/25096763 char *strtok( char *strToken, con ...
- NOI.AC #31. MST
好像又是神仙dp....gan了一早上 首先这是个计数类问题,上DP, 对于一个最小生成树,按照kruskal是一个个联通块,枚举边小到大合成的 假如当前边是树边,那么转移应该还是枚举两个块然后合并 ...
- Codeforces--400A--Inna and Choose Options(模拟水题)
Inna and Choose Options Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:26 ...
- servlet3.0 JQuary Ajax基本使用
servlet3.0 没有web.xml文件,需要使用注解进行配置. js: $(document).ready(function(){ $("#btn").click(funct ...