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
 

【题意】给出n个数,求其在第一个数到最后一个的过程中,最小的逆序数是多少

【思路】建立一颗空树,在插入每个数的时候,统计这个数前面有多少数大于他,当a[i]由第一个变为最后一个时,要加上a[i]后面大于a[i]的数的个数,有n-1-a[i]个,要减去a[i]后面小于a[i]的数的个数,有a[i]个(注意i是从0开始的)

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int N=;
struct node
{
int l,r;
int num; }tree[N*];
void build(int k,int l,int r)//建空树
{
int mid=(l+r)/;
tree[k].l=l;
tree[k].r=r;
tree[k].num=;
if(l==r) return ;
build(k*,l,mid);
build(k*+,mid+,r);
}
void updata(int k,int c)//插入
{
if(tree[k].l==c&&tree[k].r==c)
{
tree[k].num=;
return ;
}
int mid=(tree[k].l+tree[k].r)/;
if(c<=mid)
updata(k*,c);
else updata(k*+,c);
tree[k].num=tree[k*].num+tree[k*+].num;
}
int get_sum(int k,int c,int n)//统计
{
if(c<=tree[k].l&&tree[k].r<=n) return tree[k].num;
else
{
int mid=(tree[k].l+tree[k].r)/;
int sum1=,sum2=;
if(c<=mid)
sum1=get_sum(k*,c,n);
if(n>mid)
sum2=get_sum(k*+,c,n);
return sum1+sum2;
}
} int main()
{
int n;
while(scanf("%d",&n)>)
{
int a[N];
build(,,n-);
int ans=;
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
ans+=get_sum(,a[i]+,n-);
//每输入一个数时,检验一下先他输入的并比他大的数的个数,对于前面比他大的数来说,他是他们的逆序数
updata(,a[i]);
}
int minx=ans;
for(int i=;i<n;i++)
{
ans=ans+n-*a[i]-; //当a[i]由第一个变为最后一个时,要加上a[i]后面大于a[i]的数的个数,有n-1-a[i]个,要
if(ans<minx) //减去a[i]后面小于a[i]的数的个数,有a[i]个(注意i是从0开始的)
minx=ans;
}
printf("%d\n",minx);
}
return ;
}
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=+;
int a[N],b[N],c[N],ans[N];
int n,cnt,sum;
int lowbit(int x)
{
return x&(-x);
}
int query(int x)
{
int res=;
x++;
while(x<=n)
{
res+=c[x];
x+=lowbit(x);
}
return res;
}
void update(int x)
{
while(x)//对在他前且比他小的数,逆序数加1;
{
c[x]++;
x-=lowbit(x);
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(a,,sizeof(a));
memset(c,,sizeof(c));
int sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
update(a[i]+);
sum+=query(a[i]+);
}
int tmp=,s=sum;
for(int i=;i<=n;i++)
{
tmp=sum-a[i]+(n-a[i]-);
sum=tmp;
if(tmp<s)
s=tmp;
}
printf("%d\n",s);
}
return ;
}

Minimum Inversion Number_线段树||树状数组的更多相关文章

  1. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  2. HDU 1394 Minimum Inversion Number(线段树 或 树状数组)

    题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...

  3. hdu 1394 Minimum Inversion Number 逆序数/树状数组

    Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...

  4. HDU-1394 Minimum Inversion Number 线段树+逆序对

    仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...

  5. hdu1394(Minimum Inversion Number)线段树

    明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...

  6. HDU_1394_Minimum Inversion Number_线段树求逆序数

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

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

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

  8. hdu - 1394 Minimum Inversion Number(线段树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...

  9. hdu 13394 Minimum Inversion Number 线段树

    题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...

随机推荐

  1. MessageDigest简介

    一.概述 MessageDigest 类为应用程序提供信息摘要算法的功能,如 MD5 或 SHA 算法.信息摘要是安全的单向哈希函数,它接收任意大小的数据,并输出固定长度的哈希值. MessageDi ...

  2. MATLAB时间序列预测Prediction of time series with NAR neural network

    具体请参考:http://lab.fs.uni-lj.si/lasin/wp/IMIT_files/neural/nn05_narnet/ 神经网络预测时间序列数据,有三种模型, 这里是给出的是第二种 ...

  3. 关于php用simplexml_load_string解析xml出现乱码的小结

    最近在做项目时需要通过xml接口读取合作伙伴数据到数据库,在xml解析环节出现有些特殊中文字符乱码的现象.后采取下面的办法终于解决. 1.curl 抓取过来的字符是unicode编码,需要先转换为ut ...

  4. [Jquery]导航菜单效果-纵向

    $( document ).ready( function(e){ var $catCont = $( ".cat-cont" );    //二级菜单div    var $ca ...

  5. jQuery中的quickExpr

    jQuery 源码中的 quickExpr 是用来检测参数 selector 是否是复杂的 HTML 代码(如“abc<div>”)或 #id,匹配结果存放在数组 match 中 // A ...

  6. Servlet作业--实现注册和登录

    1.注册页面  zhuce.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  7. 最简单的PHP socket echo server。

    常有人困惑php的socket服务,现在有libevent和多线程了,但是我还是整一个select的 <?php $addr = '0.0.0.0'; $port = 1234; $socket ...

  8. node开发 npm install -g express-generator@4

    Node forever : 1,forever start --uid test start app.js 2,forever start --uid test start -a app.js 3, ...

  9. sql alter表字段处理

    --添加字段 ALTER table WCOLLECTION add CLT_ID int null default(0) --将已有字段类型为 NULL 修改为 NOT NULLalter tabl ...

  10. Problem A CodeForces 556A

    Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think ab ...