Minimum Inversion Number

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

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
 
       求逆序数,这道题花了我一下午的时间去看线代,不过还好总算做出了....切克闹,切脑壳...
下 面来详细讲讲过程吧...
        首先,我求出了 Simple output 给出的 序列的 逆序数为22 这是没有错的,但是输出却为16,当时我这个小脑袋呀,真是....泪崩了呀!. 
 然后我就在这里纠结呀...哎,由于英语不是很好,居然没有读懂这句话的意思.....这是啥情况 ,妈蛋呀!
     out of the above sequences.  ------>从上面的式子中找出最小的逆序数...
     明白了这句话,下面就好办了..
    最后就是一点要说的是... 对于逆序数,如果存在左右顶端对调,并且这个序列是连续的..
是可以总结出规律的,,
看代码就知道了。。
                             time   300+ms....   c++ 
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 5000
int a[maxn+];
int bb[maxn+]; //存储单个元素的逆序数
int main()
{
int n,i,j,tol;
while(scanf("%d",&n)!=EOF)
{
memset(bb,,sizeof(bb));
for(i=;i<n;i++)
{
scanf("%d",a+i);
for(j=i-;j>=;j--)
{
if(a[i]>a[j]&&bb[j]==) break;
if(a[i]<a[j])bb[i]++;
}
}
tol=;
for(i=;i<n;i++) //求出逆序数
tol+=bb[i];
int res=tol;
for(i=;i<n;i++)
{
tol+=n-*a[i]- ;
if(res>tol)
res=tol;
}
printf("%d\n",res);
} return ;
}

运用递归调用版的归并排序

比如 5 4 3 2 1 《5 ,4》,《3 ,2》  --》+ 2

 4 5 2 3 1
4 5 2 3  ---》 2 +2=4;
2 3 4 5 1 --》 4
10
运用这个原理便可以得到结果,代码如下:
 #include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define maxn 5000
int aa[maxn+];
int bb[maxn+];
int nn,tol=;
void mergec(int low ,int mid ,int hight )
{
int i,j,k;
int *cc = (int *)malloc(sizeof(int)*(hight-low+));
i=low;
j=mid;
k=;
while( i<mid&&j<hight )
{
if(aa[i]>aa[j])
{
cc[k++]=aa[j++];
tol+=mid-i;
}
else
cc[k++]=aa[i++];
}
for( ; i<mid ;i++)
cc[k++]=aa[i];
for( ; j<hight ; j++)
cc[k++]=aa[j];
k=;
for(i=low;i<hight;i++)
aa[i]=cc[k++];
free( cc );
}
/*用递归求解归并排序无法求逆序数*/
void merge_sort(int st,int en)
{
int mid;
if(st+<en)
{
mid=st+(en-st)/;
merge_sort(st,mid);
merge_sort(mid,en);
mergec(st,mid,en);
}
}
int main()
{
int i,res;
// freopen("test.in","r",stdin);
while(scanf("%d",&nn)!=EOF)
{
tol=;
for(i=;i<nn;i++){
scanf("%d",aa+i);
bb[i]=aa[i];
}
merge_sort(,nn);
res=tol;
//printf("tol=%d\n",res);
for(i=;i<nn-;i++)
{
tol+=nn-*bb[i]-;
if(res>tol) res=tol;
}
printf("%d\n",res);
}
return ;
}

接下来是非递归调用....版的归并排序

 #include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define maxn 5000
int aa[maxn+];
int bb[maxn+];
int nn,tol=;
void mergec(int low ,int mid ,int hight )
{
int i,j,k;
int *cc = (int *)malloc(sizeof(int)*(hight-low+));
i=low;
j=mid;
k=;
while( i<mid&&j<hight )
{
if(aa[i]>aa[j])
{
cc[k++]=aa[j++];
tol+=mid-i;
}
else
cc[k++]=aa[i++];
}
for( ; i<mid ;i++)
cc[k++]=aa[i];
for( ; j<hight ; j++)
cc[k++]=aa[j];
k=;
for(i=low;i<hight;i++)
aa[i]=cc[k++];
free( cc );
} /*----------------------华丽丽的分割线--------------------------------*/
void merge_sort( int st , int en )
{
int s,t,i;
t=;
while(t<=(en-st))
{
s=t;
t=s*; //表示两个s的长度
i=st;
while(i+t<=en){
mergec(i,i+s,i+t);
i+=t;
}
if(i+s<en)
mergec(i,i+s,en);
}
if(s<en-st)
mergec(st,st+s,en);
}
int main()
{
int i,res;
// freopen("test.in","r",stdin);
while(scanf("%d",&nn)!=EOF)
{
tol=;
for(i=;i<nn;i++){
scanf("%d",aa+i);
bb[i]=aa[i];
}
merge_sort(,nn);
res=tol;
//printf("tol=%d\n",res);
for(i=;i<nn-;i++)
{
tol+=nn-*bb[i]-;
if(res>tol) res=tol;
}
printf("%d\n",res);
}
return ;
}
 用树状数组...
代码:
 /*
用树状数组求逆序数
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 5000
int aa[maxn+];
int bb[maxn+];
int nn;
int lowbit(int k)
{
return k&(-k);
}
void ope(int x)
{
while(x<=nn)
{
aa[x]++;
x+=lowbit(x);
}
}
int sum(int x)
{
int ans=;
while(x>)
{
ans+=aa[x];
x-=lowbit(x);
}
return ans;
}
int main()
{ int i,res,ans;
//freopen("test.in","r",stdin);
while(scanf("%d",&nn)!=EOF)
{
memset(aa,,sizeof(aa));
res=;
for(i=;i<nn;i++)
{
scanf("%d",&bb[i]);
res+=sum(nn)-sum(bb[i]+);
ope(bb[i]+);
}
ans=res;
for(i=;i<nn;i++)
{
res+=nn--*bb[i];
if(ans>res)
ans=res;
}
printf("%d\n",ans);
}
return ;
}

HUDOJ-----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

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

  3. 1394-Minimum Inversion Number

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

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

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

  5. HDU 1394-Minimum Inversion Number(BIT)

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

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

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

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

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

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

  10. HDU-Minimum Inversion Number(最小逆序数)

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

随机推荐

  1. 阿里云96页报告详解《云上转型》(10个案例、10大趋势/完整版PPT)

    阿里云96页报告详解<云上转型>(10个案例.10大趋势/完整版PPT) 2017-12-29 14:20阿里云/云计算/技术 ﹃产业前沿超级干货﹄ ﹃数据观○重磅速递﹄ 阿里云研究中心云 ...

  2. Redis:ERR operation not permitted

    Redis服务开启之后: D:\Redis\redis>redis-server.exe redis.conf 那么另外开一个命令窗口 D:\Redis\redis>redis-cli.e ...

  3. 深入C++的new

    new”是C++的一个关键字,同时也是操作符.关于new的话题非常多,因为它确实比较复杂,也非常神秘,下面我将把我了解到的与new有关的内容做一个总结. new的过程 当我们使用关键字new在堆上动态 ...

  4. Search a 2D Matrix leetcode java

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  5. UNdelete

    --90兼容模式以上,2005+ -- http://raresql.com/2012/10/24/sql-server-how-to-find-who-deleted-what-records-at ...

  6. Database Setup

    Database Setup This library has been developed so that you can use any type of backend storage; rela ...

  7. Faiss学习:一

    在多个GPU上运行Faiss以及性能测试 一.Faiss的基本使用 1.1在CPU上运行 Faiss的所有算法都是围绕index展开的.不管运行搜索还是聚类,首先都要建立一个index. import ...

  8. 破解无线网络密码-BT3如何使用3

    BT3 虚拟机 SNOOPWEP2 破解无线网络WEP密钥图解 1.下载BT3 光盘映像文件(ISO格式),比如:bt3-final.iso: 用WinISO 或 UltraISO(这个还支持DVD ...

  9. Windows删除无效服务

    W+R运行:regedit http://os.51cto.com/art/201208/354989.htm

  10. jquery局部打印插件使用

    基于jquery库的jquery.PrintArea.js插件源代码为: (function ($) { var printAreaCount = 0; $.fn.printArea = functi ...