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.

InputThe 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. 
OutputFor 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个数的整数序列,每次可将序列首个数放到最后一位,其余数相对位置不变,通过这样的操作可以得到n种序列,要求这n种序列中倒置数的最小个数,即满足下标i<j,a[i]>a[j]。

其实说白了就是求不停倒置的最小逆序数

求逆序数用线段树或者树状数组都是挺方便的,每次加一个数进去,看此时后面的数有几个。

但这样肯定会超时,我们可以首先写出一开始的逆序数,发现每次倒置后,逆序数为减少a[i],而增加n-1-a[i]的。减少比它小的,增加比它大的

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int a[maxn<<];
int add[maxn];
#define lson l,m,k<<1
#define rson m+1,r,k<<1|1
void hehe(int k)
{
a[k]=a[k<<]+a[k<<|];
}
void build(int l,int r,int k)
{
a[k]=;//先是假设这棵树都为0
if(l==r) return ;
int m=(l+r)>>;
build(lson);
build(rson);
}
int query(int x,int y,int l,int r,int k)
{
if(x<=l&&r<=y)
return a[k]; int m=(l+r)>>;
int s=;
if(x<=m) s+=query(x,y,lson);
if(y>m) s+=query(x,y,rson);
return s;
}
void update(int p,int l,int r,int k)
{
if(l==r)
{
a[k]++;//增加个数
return;
}
int m=(l+r)>>;
if(p<=m) update(p,lson);
else update(p,rson);
hehe(k);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
build(,n-,);
int sum=;//为一开始的逆序数
for(int i=;i<n;i++)
{
scanf("%d",&add[i]);
sum+=query(add[i],n-,,n-,);
update(add[i],,n-,);
}
int minn=sum;
for(int i=;i<n;i++)
{
sum+=n-add[i]-add[i]-;
minn=min(minn,sum);
}
cout<<minn<<endl;
}
return ;
}

HDU 1394Minimum Inversion Number的更多相关文章

  1. HDU 1394Minimum Inversion Number 数状数组 逆序对数量和

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

  2. HDU 1394Minimum Inversion Number(线段树)

    题目大意是说给你一个数组(N个),没戏可以将其首部的k(k<N)个元素移动至尾部,这样总共会形成N个序列 现在要求这n个序列中逆序对数最少的那一个序列有多少个逆序对 最初的确是没太多思路,就算知 ...

  3. HDU 1394-Minimum Inversion Number(BIT)

    题意: 给你n个数字的序列 每次把第一个数字放到最后 得到一个新序列 一共有n个序列求这些序列中哪个序列含最小的总的逆序数 (输出最小总逆序数) 分析: 用BIT求出初始各数的逆序数,第一个数放最后它 ...

  4. 1394-Minimum Inversion Number

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

  5. hdu Minimum Inversion Number(逆序数的小知识与线段树)

    飞! 题解 首先,求逆序数对的思路: 1.得到整个数列后,从前往后扫,统计比a[i]小的,在a[i]后面的有多少个 这样做的话,应该是只有n2的暴力作法,没想到更好的方法 2.统计a[i]前面的,且比 ...

  6. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  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 - 树状数组

    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...

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

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

随机推荐

  1. react系列(三)组件间通信

    组件间通信 React的基本组件元素是一个个组件,组件之间可能存在关联.组合等关系.不同的组件之间,经常会发生数据传递或者交换,我们称之为组件间通信. 根据传递的复杂程度,可以分为三种情况: 父子间通 ...

  2. Nginx负载均衡+代理+ssl+压力测试

    一.Tomcat安装 1.下载jdk,Tomcat,解压到/usr/local/ 2.配置jdk环境: # vim /etc/profile export JAVA_HOME=/usr/local/j ...

  3. NodeJ node.js Jquery Ajax 跨域请求

    Jquery + Ajax 跨域请求 说白了就是前台请求ajax数据(JSON)但是请求的数据不在本地的绝对路径下,接口数据 是没有这个安全性的我对外公开的接口数据,只要你找到接口你就可以使用里面的数 ...

  4. div样式position:fixed,不随屏幕滚动而滚动,导致屏幕太小时弹出层被遮挡,无法滚动查看的解决办法

    window.onscroll = function () { var sl = -Math.max(document.body.scrollTop, document.documentElement ...

  5. 微信小程序新版用户授权方式处理

    最新更新(2018-12-27): 最近做了改版,做成默认进来就是首页,然后去判断有没有用户信息,没有的话再去判断用没授权过,如果授权过直接自动去获取,没有的话再跳转到授权页面.因为用户授权主要就是针 ...

  6. CASE WHEN 批量更新

    单个值: UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHE ...

  7. python在lxml中使用XPath语法进行#数据解析

    在lxml中使用XPath语法: 获取所有li标签: from lxml import etree html = etree.parse('hello.html') print type(html) ...

  8. 从python2.x到python3.x进阶突破

    1.p2是重复代码,语言不同,不支持中文;p3则相反,其中代码不重复,语言用的相同的,并且是支持中文的. 2.p2中input中输入数字输出数字,输入字符串必须自己手动加引号才行;p3中input输出 ...

  9. Educational Codeforces Round 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence

    这个题是dp, dp[i]代表以i开始的符合要求的字符串数 j是我们列举出的i之后一个字符串的开始地址,这里的C是组合数 dp[i] += C(j - i - 1, A[i]] )* dp[j]; # ...

  10. tomcat8.5配置高并发

    最近部署的tomcat应用,有一天压测的时候,测试一致反馈下载不了,结果查看日志才发现如下错误: INFO: Maximum number of threads (200) created for c ...