Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14952    Accepted Submission(s): 9140

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
 题解;题意就是每次把序列的第一个元素放到最后,求最小逆序数;建设当前值为x则,每次逆序数减小x,增加N-1-x;因为x比x个数大,比N-1-X个数小;
代码:
 #include<stdio.h>
#include<string.h>
#define MIN(x,y)(x<y?x:y)
const int MAXN=;
int a[MAXN],b[MAXN],anser,c[MAXN];
void mergesort(int l,int r,int mid){
int i=l,j=mid+,k=l;
while(i<=mid&&j<=r){
if(a[i]<=a[j])b[k++]=a[i++];
else{
anser+=j-k;
b[k++]=a[j++];
}
}
while(i<=mid)b[k++]=a[i++];
while(j<=r)b[k++]=a[j++];
for(i=l;i<=r;i++)a[i]=b[i];
}
void merge(int l,int r){
if(l<r){
int mid=(l+r)>>;
merge(l,mid);
merge(mid+,r);
mergesort(l,r,mid);
}
}
int main(){
int N;
while(~scanf("%d",&N)){
for(int i=;i<N;i++)scanf("%d",a+i),c[i]=a[i];
anser=;
merge(,N-);
int temp=anser;
//printf("%d\n",anser);
for(int i=;i<N;i++){
temp=temp+(N--*c[i]);
//printf("%d %d\n",c[i],temp);
anser=MIN(anser,temp);
}
printf("%d\n",anser);
}
return ;
}
//n-a-1-(a) N-2*a+1;

Minimum Inversion Number(归并排序)的更多相关文章

  1. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  2. HDU1394 Minimum Inversion Number(线段树OR归并排序)

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

  3. HDU 1394 Minimum Inversion Number(树状数组/归并排序实现

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

  4. 逆序数2 HDOJ 1394 Minimum Inversion Number

    题目传送门 /* 求逆序数的四种方法 */ /* 1. O(n^2) 暴力+递推 法:如果求出第一种情况的逆序列,其他的可以通过递推来搞出来,一开始是t[1],t[2],t[3]....t[N] 它的 ...

  5. Minimum Inversion Number(线段树求逆序数)

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

  6. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

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

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

  8. HDU 1394 Minimum Inversion Number(最小逆序数 线段树)

    Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...

  9. ACM Minimum Inversion Number 解题报告 -线段树

    C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

随机推荐

  1. Emacs配置erlang开发环境(.emacs 文件)

    以前都是用sublime写erlang代码,好处不多说,主要是觉得一点不好用,不能实现函数跳转,及其不方便,尤其是代码一多,头疼.后来折腾过IntelliJ,下了个收费$0.00的版本,风格还是挺稀饭 ...

  2. Windows Azure 社区新闻综述(#68 版)

    欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure 的社区推动新闻.内容和对话. 以下是过去一周基于您的反馈汇集在一起的内容: 文章.视频和博客文章 在 Windows Az ...

  3. Hibernate 1、Hello Hibernate

    所使用到的jar 包: 1.创建实体类 public class User { private Integer id; private String name; private String addr ...

  4. ORA-07445 [mdagun_iter+957] When Using SDO_AGGR_UNION 问题处理

    问题描写叙述: ORA-07445: mdagun_iter()  [Address not mapped to object] Oracle Database 10g Enterprise Edit ...

  5. DrawText的使用

    DrawText函数简介 这个函数的作用非常easy,就是在指定的区域内输出格式化的文本. 函数原型: int DrawText( HDC hDC, LPCTSTR lpString, int nCo ...

  6. 学习Java这几个快捷键你得知道(不断更新中)

    java中的System.out.println();的快捷键    --------先输入sysout  在按 alt + /

  7. 批处理就是windows的杰作啊

    今天要为了解决vs不能同时开启调试和编写的问题,我就上网查找了一些批处理的命令,用批处理调用exe,和打开txt,虽然一行代码就解决了但是我没用过啊,很陌生. call  路径\a.exe  就相当于 ...

  8. meta标签的少许语法,慢慢收集中...

    收集了一些meta的语法,也将不断的更新.不断做点滴的收集,总之,为了前端这些东西呀,我也是操碎了心... 1 <meta http-equiv="Content-Type" ...

  9. 在spring中进行基于Executor的任务调度

    Executor java.util.concurrent.Executor接口的主要目的是要将“任务提交”和“任务执行”两者分离解耦.该接口定义了任务提交的方法,实现者可以提供不同的任务运行机制,解 ...

  10. root cause:org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException

    今天在调试SSH与Ajax时,服务器端报出JSON异常: