B - Minimum Inversion Number

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

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

本题的意思是求逆序数,可以将前m个数放到序列最后面,求逆序数的最小值
方法就是找出每个数之前有多少个比它大的数累加即可,处理时可以用树状数组,也可以用线段树,我用的是线段树
先建一棵空树,依次处理每个元素,把线段树的对应节点a[i]++,然后在原序列中,在a[i]前面且比它大的就是在线段树中
a[i+1]到n这段区间和了;
这样容易找原序列的逆序数,至于调动是有个规律的,每次把第一个移到后面,逆序数就等于原来的-(后面比a小的)+(比a大的)




代码如下




#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f int a[5005];
int tree[5005*4]; void build(int n,int l,int r)
{
if(l==r)
{
tree[n]=0;
return;
}
int m=(l+r)>>1;
build(n<<1,l,m);
build(n<<1|1,m+1,r);
tree[n]=tree[n<<1]+tree[n<<1|1];
} void update(int n,int l,int r,int pos)
{
if(l==r)
{
tree[n]++;
return;
}
int m=(l+r)>>1;
if(pos<=m)
update(n<<1,l,m,pos);
else
update(n<<1|1,m+1,r,pos);
tree[n]=tree[n<<1]+tree[n<<1|1];
} int query(int n,int l,int r,int ll,int rr)
{
if(ll<=l&&rr>=r)
{
return tree[n];
} int ans=0;
int m=(l+r)>>1;
if(ll<=m)
ans+=query(n<<1,l,m,ll,rr);
if(rr>m)
ans+=query(n<<1|1,m+1,r,ll,rr);
return ans;
} int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
a[i]++;
}
build(1,1,n);
int cnt=0;
for(int i=1; i<=n; i++)
{
update(1,1,n,a[i]);
cnt+=query(1,1,n,a[i]+1,n);
} int mx=cnt;
for(int i=1; i<=n; i++)
{
cnt=cnt-(a[i]-1)+(n-a[i]);
mx=min(mx,cnt);
}
printf("%d\n",mx);
}
return 0;
}





HDU1349 Minimum Inversion Number 2016-09-15 13:04 75人阅读 评论(0) 收藏的更多相关文章

  1. js笔记 标签: javascript 2016-08-01 13:30 75人阅读 评论(0) 收藏

    typeof可以用来检测给定变量的数据类型,typeof是一个操作符而不是函数,所以圆括号可以省略. Undefined类型只有一个值,即特殊的undefined.在使用var声明变量但未对其加以初始 ...

  2. Segment Tree 分类: ACM TYPE 2014-08-29 13:04 97人阅读 评论(0) 收藏

    #include<iostream> #include<cstdio> using namespace std; struct node { int l, r, m; int ...

  3. Gold Coins 分类: POJ 2015-06-10 15:04 16人阅读 评论(0) 收藏

    Gold Coins Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21767   Accepted: 13641 Desc ...

  4. Codeforces805D. Minimum number of steps 2017-05-05 08:46 240人阅读 评论(0) 收藏

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

  5. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

  6. Power Network 分类: POJ 2015-07-29 13:55 3人阅读 评论(0) 收藏

    Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24867 Accepted: 12958 Descr ...

  7. HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏

    Red and Black Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  8. Codeforces 766C Mahmoud and a Message 2017-02-21 13:57 62人阅读 评论(0) 收藏

    C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. Eclipse 快捷键大全 分类: C_OHTERS 2014-06-01 13:05 332人阅读 评论(0) 收藏

      精选常用: 1.  ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如a ...

随机推荐

  1. HDFS 好的文章链接

    http://www.cnblogs.com/linuxprobe/p/5594431.html http://www.daniubiji.cn/archives/596 http://blog.cs ...

  2. The maximum column size is 767 bytes (Mysql)

     ERROR app.wsutils 419 INCRON: Error: ('HY000', '[HY000] [MySQL][ODBC 5.2(w) Driver][mysqld-5.7.7-rc ...

  3. GEOquery

    1)介绍 来自NCBI的Gene Expression Omnibus(GEO)作为各种高通量实验数据的公共存储库. 这些数据包括基于单通道和双通道微阵列的实验,测量mRNA,基因组DNA和蛋白质丰度 ...

  4. CentOS6.5下安装ActiveMQ

    1.下载ActiveMQ [root@localhost softwares]# wget http://archive.apache.org/dist/activemq/apache-activem ...

  5. poj1015 正解--二维DP(完全背包)

    题目链接:http://poj.org/problem?id=1015 错误解法: 网上很多解法是错误的,用dp[i][j]表示选择i个人差值为j的最优解,用path[i][j]存储路径,循环次序为“ ...

  6. string类小结

    要想使用标准C++中string类,必须要包含 #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 ...

  7. keras—多层感知器识别手写数字算法程序

    #coding=utf-8 #1.数据预处理 import numpy as np #导入模块,numpy是扩展链接库 import pandas as pd import tensorflow im ...

  8. Spring配置连接池

    ---------------------siwuxie095                                 Spring 配置连接池         1.Spring 配置内置连接 ...

  9. c++流操作

    非缓冲标准出错流对象cerr和缓冲标准出错流对象clog,它们都是来自于ostream类的对象,用于输出错信息.cerr和clog之间的不同之处在于cerr是不经过缓冲区直接向显示器输出有关信息,而c ...

  10. Python学习day5作业

    目录 Python学习day5作业 ATM和购物商城 1. 程序说明 2. 基本流程图 3. 程序测试帐号 4. 程序结构: 5. 程序测试 title: Python学习day5作业 tags: p ...