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

Problem Description
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.

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.

 
Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
 
Output
For each case, output the minimum inversion number on a single line.
 
Sample Input
10
1 3 6 9 0 8 5 7 4 2
 
Sample Output
16
 
线段树求逆序数,手撸。
先求初始序列的逆序数(nlogn),再推出所有情况,求的最小值。
#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_线段树求逆序数的更多相关文章

  1. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  2. hdu1394--Minimum Inversion Number(线段树求逆序数,纯为练习)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  3. <Sicily>Inversion Number(线段树求逆序数)

    一.题目描述 There is a permutation P with n integers from 1 to n. You have to calculate its inversion num ...

  4. HDU-1394 Minimum Inversion Number(线段树求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  5. 线段树求逆序数方法 HDU1394&amp;&amp;POJ2299

    为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...

  6. hdu 1394 (线段树求逆序数)

    <题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...

  7. HDU - 1394 Minimum Inversion Number (线段树求逆序数)

    Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs ( ...

  8. 【线段树求逆序数】【HDU1394】Minimum Inversion Number

    题目大意: 随机给你全排列中的一个,但不断的把第一个数丢到最后去,重复N次,形成了N个排列,问你这N个排列中逆序数最小为多少 做法: 逆序数裸的是N^2 利用线段树可以降到NlogN 具体方法是插入一 ...

  9. hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)

    题目传送门 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

随机推荐

  1. 源码分析-react2-根节点渲染

    //FiberNode{ alternate : '通过该属性和后面的切片进行比较', child : '改切片的子切片', firstEffect : '当前要加入的切片', stateNode : ...

  2. Oracle Multitenant Environment (四) Create One or More CDBs

    Using the CREATE DATABASE Statement to Create a CDB This section describes creating a CDB using the  ...

  3. jQuery -&gt; 获取/设置HTML或TEXT内容

    jQuery提供了两个API能够直接用来为元素加入内容. html() text() 当中html()是为指定的元素加入html内容 text()是为指定的元素加入文本内容 两者的差别在于,text中 ...

  4. datatables接口

    /*资源表格接口*/ var dataTableHeader=function(elem,unSorts,defaultSort,screens,status,toggleVis,ipAddress, ...

  5. Longest Increasing Subsequence HDU - 6284

    /* 首先预处理好f g数组 fi :以a[i]为结尾的 最长上升子序列的长度 gi :以a[i]为开始的 最长上升子序列的长度 mxx : 最长上升子序列的长度 线段树优化 nlogn (不包含a[ ...

  6. Bing Maps进阶系列七:Bing Maps功能导航菜单华丽的变身

    Bing Maps进阶系列七:Bing Maps功能导航菜单华丽的变身 Bing Maps Silverlight Control所提供的功能导航是非常强大的,在设计上对扩展的支持非常好,提供了许多用 ...

  7. JavaScript检查是否包含某个字符

    转自:http://my.oschina.net/u/1450300/blog/389325 indexOf用法: indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置.如 ...

  8. 48.EXt.Data.JsonReader()

    转自:https://blog.csdn.net/huoyanxueren/article/details/2662915?utm_source=blogxgwz6 extJs 2.1学习笔记(Ext ...

  9. Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST

    题目原文: Given a binary tree where each 

  10. 03、HelleBaiduMap

    D:\百度地图\百度地图\资料\百度地图与定位SDK\百度地图v3.5.0\BaiduMap_AndroidSDK_v3.5.0_All\BaiduMap_AndroidSDK_v3.5.0_Docs ...