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. OpenWRT编译记录--TPLINK_WR841ND_V7

    之前自己编译OpenWRT的一些记录,现在搬上来.简单介绍了编译环境的准备,编译过程,以及一些注意事项. 准备工作 本人是在Ubuntu环境下编辑的,首先安装编译所需要的组件包: sudo apt-g ...

  2. kafka_2.11-0.10.0.0安装步骤

    Kafka安装配置 我们使用5台机器搭建Kafka集群: 1. cluster-1-namenode-1-001       172.16.0.147 2. cluster-1-datanode-1- ...

  3. 本地缓存FMDB的使用(iOS)

    一.简单说明 1.什么是FMDB FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API 2.FMDB的优点 使用起来更加面向对象,省去了很多麻烦.冗余的C ...

  4. java 垃圾回收总结(可达性分析 引用分类

    java 垃圾回收总结(1)   以前看过很多次关于垃圾回收相关的文章,都只是看过就忘记了,没有好好的整理一下,发现写文章可以强化自己的记忆. java与C,c++有很大的不同就是java语言开发者不 ...

  5. TortoiseGit 错误信息Aborting commit due to empty commit message.解决

    错误信息: Aborting commit due to empty commit message. git不能完全退出(退出码 1) (47 ms @ 2016/2/19 14:03:24) 解决办 ...

  6. C#中的??是什么意思

    C#中的??是什么意思 DJ8Angus | 浏览 49982 次  2012-01-16 12:07 2012-01-16 12:23   最佳答案   如果不赋予初值,C#的变量是不允许直接使用的 ...

  7. Python编码问题整理【转】

    认识常见编码 GB2312是中国规定的汉字编码,也可以说是简体中文的字符集编码 GBK 是 GB2312的扩展 ,除了兼容GB2312外,它还能显示繁体中文,还有日文的假名 cp936:中文本地系统是 ...

  8. iOS二维码条形码的制作

    - (IBAction)creatQRImage:(id)sender { [self.textField resignFirstResponder]; //这里是我放了个TextField的控件,但 ...

  9. POJ 2484 A Funny Game(找规律)

    题目链接 #include<iostream> #include<cstdio> using namespace std; int main() { int n; while( ...

  10. SSH 两个表全套增删改(运动员住宿管理)

    0.创建如下oracle的命令 create table HOTALINFO ( HOTALID ) not null, HOTALNAME ) not null, HOTALADDRESS ) no ...