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
 
 
//有的大神是用线段树解的,原谅我怎么都无法看出线段树来。。。。_(:з」∠)_
//于是采用暴力解,然而。。超时了   (╯‵□′)╯︵┴─┴
//于是借鉴别人的代码~~~  (●'◡'●)
//思路:每次把末尾的数掉到序列前面时,减少的逆序对数为n-1-a[i] ,

//增加的逆序对数为a[i] ,这样就可在所有的序列中找出含有逆序对最少的了!(ps:给跪了,这是性质吗。。。
 
#include <iostream>
#include <cstdio>
using namespace std; int main()
{
int n,ans,k;
int data[];
while(cin>>n)
{
ans=;
for(int i=;i<n;i++)
scanf("%d",&data[i]);
for(int i=;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(data[i]>data[j])
ans++;
}
}
k=ans;
for(int i=n-;i>=;i--)
{
k-=n--data[i];
k+=data[i];
if(ans>k)
ans=k;
}
cout<<ans<<endl;
}
return ;
}
//再贴上我的狗血超时代码。。。
 

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; int find(int a[],int n)
{
int ans=;
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
{
if(a[i]>a[j])
ans++;
}
}
return ans;
} int main()
{
int n;
int data[],num[];
while(scanf("%d",&n)!=-)
{
for(int i=;i<n;i++)
scanf("%d",&data[i]);
for(int i=;i<n;i++)
{
num[i]=find(data,n);
int tmp=data[n-];
for(int j=n-;j>=;j--)
{
data[j]=data[j-];
}
data[]=tmp;
}
sort(num,num+n);
cout<<num[]<<endl;
}
return ;
}

hdu 1394 Minimum Inversion Number(这道题改日我要用线段树再做一次哟~)的更多相关文章

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

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

  2. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

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

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

  4. hdu 1394 Minimum Inversion Number - 树状数组

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

  5. HDU 1394 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 逆序数/树状数组

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

  7. hdu 1394 Minimum Inversion Number(逆序数对) : 树状数组 O(nlogn)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394  //hdu 题目   Problem Description The inversion number ...

  8. HDU 1394——Minimum Inversion Number——————【线段树单点增减、区间求和】

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

  9. HDU 1394 Minimum Inversion Number (树状数组)

    题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...

随机推荐

  1. HDU2639[背包第K大]

    题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=2639] 题意:求第k大背包. 题解:利用二路归并的思想,求解第K大的值. #include<bi ...

  2. 重载 vs 重写

    http://www.cnblogs.com/lonelyDog/archive/2011/11/16/2251011.html

  3. Redis断线重连编码注意事项

    应用在Redis重启.网络闪断并恢复正常后,应用必须能够自恢复,下面以Java语言的jedis客户端为例说明: 1.作为发布者 Jedis对象不能作为单例,网络闪断后该Jedis对象无法自恢复.应该每 ...

  4. [妙味Ajax]第一课:原理和封装

    知识点总结: ajax是异步的javascrip和xml,用异步的形式去操作xml 访问的是服务端,即https://127.0.0.1/ 或者 https://localhost 1.创建一个aja ...

  5. 2016WHD.china世界云计算日·北京站即将召开

    WHD.china自进驻中国以来,已在上海.北京成功举办多届,2015年于北京举办的会议更是盛况空前,注册参会者逾800人次,汇聚了国内外众多知名云服务商.IDC商.电子商务企业.电信运营商.ISP等 ...

  6. javascript apply()和call()

    原文链接 http://www.jb51.net/article/30883.htm 想要理解透彻apply()和call() ,还要需要理解this  作用域 局部变量  全局变量 js apply ...

  7. CF 604C Alternative Thinking#贪心

    (- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cstring> using namespace std ...

  8. HDU 3835 R(N)(枚举)

    题目链接 Problem Description We know that some positive integer x can be expressed as x=A^2+B^2(A,B are ...

  9. REST认识

    大家对REST的认识? 谈到REST大家的第一印象就是通过http协议的GET,POST,DELETE,PUT方法实现对url资源的CRUD(创建.读取.更新和删除)操作.比如http://www.a ...

  10. Android应用测试性能的工具Emmagee,导出文件格式问题分析

    原文引用自:http://www.open-open.com/lib/view/open1367026451078.html Emmagee是监控指定被测应用在使用过程中占用机器的CPU.内存.流量资 ...