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. StackExchange.Redis 基本使用 (一) (转)

    StackExchange.Redis下载地址: https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Basic ...

  2. HSDFS fs命令

    fs命令 -help [cmd]  //显示命令的帮助信息 -ls(r) <path>  //显示当前目录下所有文件 -du(s) <path>  //显示目录中所有文件大小 ...

  3. 完全删除Postgresql

    First: If your install isn't already damaged, you can drop unwanted PostgreSQL servers ("cluste ...

  4. selenium中的webdriver定位元素失败的常见原因

    自动化测试中经常会出现无法定位元素的情况,报selenium.common.exceptions.NoSuchElementException错误 Frame/Iframe原因定位不到元素: 这个是最 ...

  5. input、button、a标签 等定义的按钮尺寸的兼容性问题

    在项目中有遇到这类问题,搜索了一下解决方式,采用链接:https://segmentfault.com/q/1010000000190931 里各位楼主的答案,摘抄如下: 例子如下: HTML: &l ...

  6. 更改自身web项目的图标(默认为tomcat的小喵咪)

    在页面<head>标签中加入 <link rel="shortcut icon" href="img/11.png" type="i ...

  7. Java IO 转换流 字节转字符流

    Java IO 转换流 字节转字符流 @author ixenos 字节流 输入字节流:---------| InputStream 所有输入字节流的基类. 抽象类.------------| Fil ...

  8. hdu_5881_Tea(xjb猜)

    题目链接:hdu_5881_Tea 题意: 有一壶水, 体积在 L 和 R 之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不 ...

  9. Opencv常用函数

    一.图像读写与简单处理 1. Mat cv::imread(const String& filename, int flags=IMREAD_COLOR). imread函数加载filenam ...

  10. 参考C++STL标准库中对了的使用方法

    http://www.cppblog.com/zhenglinbo/archive/2012/09/18/191170.html 参考:http://www.cppblog.com/zhenglinb ...