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 ;
}

最新文章

  1. HTML块级标签汇总(小篇)
  2. email 正则
  3. Hibernate控制台显示创建数据库表语句
  4. [iOS dispatch_once创建单例]
  5. C# 读取压缩文件方式及乱码处理
  6. CodeForces - 404A(模拟题)
  7. Yslow&amp;PageSpeed– 诊断各种缓慢症状
  8. 车牌识别LPR(二)-- 车牌特征及难点
  9. jQuery分析(3) - jQuery.fn.init
  10. 《算法问题实战策略》-chaper32-网络流
  11. SQL Server 日志文件增长原因定位
  12. C#读书笔记之object类的基本方法
  13. PHP面向对象之解释器模式
  14. js 获取 url 参数
  15. supervisor管理进程工具配置
  16. [administrative][lvm] lvm 分区修改
  17. Linux - awk 文本处理工具四
  18. python sorted() count() set(list)-去重 -- search + match
  19. redhat5本地源NBD驱动安装
  20. 下载 Microsoft SQL Server JDBC 驱动程序

热门文章

  1. SVN提交时报错:Commit blocked by pre-commit hook (exit code 1) with no output.
  2. Maven奇怪的问题,当找不到Maven输出的提示错误时可以试下这个方法
  3. 【Android】自己定义圆形ImageView(圆形头像 可指定大小)
  4. python 除法总返回浮点
  5. Android Studio 开发利器【经常使用插件】
  6. Git实战(三)环境搭建
  7. Linux下使用Vi是方向键变乱码 退格键不能使用的解决方法
  8. nginx安装【windows下安装】
  9. tomcat的localhost_access_log日志文件
  10. MarkMonitor 目前最安全的域名注册商,因此,世界500强网站中的22%域名托管于markmonitor公司