Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

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

Author

CHEN, Gaoli

Source

ZOJ Monthly, January 2003

Recommend

Ignatius.L | We have carefully selected several similar problems for you: 1166 1698 1540 1542 1255

看到题的第一眼还以为是动态逆序对,读完题后发现竟然是一道水题。

这道题想让我们求的是在不断改变元素的顺序之后所有逆序对的最小值。怎么做?首先我们知道在进行n" role="presentation" style="position: relative;">nn次轮换之后数列又变回去了,所以我们只需用一种高效的方法统计出每次轮换后数列逆序对的个数就行了。

怎么统计?

显然第一次逆序对直接上树状数组(线段树)求so" role="presentation" style="position: relative;">soso easy" role="presentation" style="position: relative;">easyeasy,那么之后的轮换呢?用n" role="presentation" style="position: relative;">nn次树状数组==gg" role="presentation" style="position: relative;">gggg,因此可以想到这样轮换有特殊的性质。没错就是这样。我们考虑当前队首的元素,这个元素从队首移到队尾的操作可以拆成队首弹出,队尾插入的操作,设上一次的逆序对数为sum" role="presentation" style="position: relative;">sumsum,那么这个元素从队首弹出显然会使sum" role="presentation" style="position: relative;">sumsum减少a1−1" role="presentation" style="position: relative;">a1−1a1−1,道理显然(a1" role="presentation" style="position: relative;">a1a1之后有a1−1" role="presentation" style="position: relative;">a1−1a1−1个数比它小),同理,这个元素从队尾插入会使sum" role="presentation" style="position: relative;">sumsum变大n−a1" role="presentation" style="position: relative;">n−a1n−a1(证明同理)。这样看来的话似乎一个循环比比大小就没了啊。

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 5005
using namespace std;
int n,bit[N],a[N],sum,ans;
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    return ans;
}
inline int lowbit(int x){return x&-x;}
inline void update(int x,int v){for(int i=x;i<=n;i+=lowbit(i))bit[i]+=v;}
inline int query(int x){int ans=0;for(int i=x;i;i-=lowbit(i))ans+=bit[i];return ans;}
int main(){
    while(~scanf("%d",&n)){
        sum=ans=0;
        memset(a,0,sizeof(a));
        memset(bit,0,sizeof(bit));
        for(int i=1;i<=n;++i){
            a[i]=read()+1;
            sum+=query(n)-query(a[i]);
            update(a[i],1);
        }
        ans=sum;
        for(int i=1;i<=n;++i)ans=min(ans,sum=sum-a[i]+1+n-a[i]);
        printf("%d\n",ans);
    }
    return 0;
}

2018.07.08 hdu1394 Minimum Inversion Number(线段树)的更多相关文章

  1. hdu1394(Minimum Inversion Number)线段树

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

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

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

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

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

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

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

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

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

  6. 2018.07.08 hdu6183 Color it(线段树)

    Color it Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Proble ...

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

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

  8. hdu 13394 Minimum Inversion Number 线段树

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

  9. 2018.07.08 POJ 2481 Cows(线段树)

    Cows Time Limit: 3000MS Memory Limit: 65536K Description Farmer John's cows have discovered that the ...

随机推荐

  1. 1.JSONObject与JSONArray的使用

    参考文献: http://blog.csdn.net/huangwuyi/article/details/5412500 1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib ...

  2. shutil模块(高级的文件copy)

    import shutil import os f1 = open('本节笔记.txt', encoding = 'utf-8') f2 = open('笔记2', 'w', encoding = ' ...

  3. PAXOS/RAFT理解

    PAXOS学习记录 前提: 信息准确无篡改,通信环境可信 目的: 解决多结点间一致性问题(集群中一个修改或者申请成为主结点的提议) 主要角色: Proposer :提出议案(同时存在一个或者多个,他们 ...

  4. win10 ubuntu双系统安装后无法引导进入ubuntu

    之前按照先装windows后装ubuntu的方式装的系统,都可以引导到ubuntu,无论是将ubuntu挂在到/boot在windows用easy BCD建立ubuntu引导,还是将ubuntu挂在到 ...

  5. Mybatis知识(2)

    1.#{}和${}的区别是什么? 注:这道题是面试官面试我同事的. 答:${}是Properties文件中的变量占位符,它可以用于标签属性值和sql内部,属于静态文本替换,比如${driver}会被静 ...

  6. SpringBoot application.yml文件不生效

    yml格式对缩进有严格的要求,检查你的yml配置文件是否有不合格的缩进项. 正确的格式如下: server: port: 8881 port前必须有空格,  port后的冒号 后面也需要有空格

  7. android示例:一个简单的登陆程序

    最近写了个简单的登陆程序,有几点收获: 1.懂得如何在LinearLayout中嵌套LinearLayout,完善布局的行列: 2.用android:layout_weight控制控件的比重: 3.用 ...

  8. OC 单例实现

    2. 在.h 文件遵循 <NSCopying,NSMutabalecopying> 3.定义宏,实现任意类型单单例 #define SingleH(name) +(instancetype ...

  9. 【转】从源码浅析MVC的MvcRouteHandler、MvcHandler和MvcHttpHandler

    原文:http://www.cnblogs.com/jeffwongishandsome/archive/2012/01/08/2316521.html 熟悉WebForm开发的朋友一定都知道,Pag ...

  10. JTemplate学习(一)

    使用模板绑定数据,可以嵌套循环 参考:http://www.doc88.com/p-6621237324128.html <!DOCTYPE html PUBLIC "-//W3C// ...