Minimum Inversion Number

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

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
 题意:
给长度是n的序列,每次将第一个数放到最后,直到数列再次变成最初的数列,求过程中最小的逆序数。
代码:
//求出最初的逆序数sum,每次操作将a[i]放到最后时更新sum:
//加上a[i]向后的顺序数(比他大的数的个数),减去a[i]向后的
//逆序数(比她小的数的个数),加上a[i]向前的(他前面的数放到后面了)
//顺序数减去a[i]向前的逆序数。所以两遍树状数组就解决了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int n,c[maxn*+],a[maxn+],sh[maxn+],ni[maxn+];
int lowbit(int x) {return x&(-x);}
void add(int id)
{
while(id<=){
c[id]++;
id+=lowbit(id);
}
}
int query(int id)
{
int s=;
while(id>){
s+=c[id];
id-=lowbit(id);
}
return s;
}
int main()
{
int n;
while(scanf("%d",&n)==){
int sum=;
memset(c,,sizeof(c));
memset(sh,,sizeof(sh));
memset(ni,,sizeof(ni));
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
a[i]++;
add(a[i]);
sh[i]=query(a[i]-);
ni[i]=i-sh[i]-;
sum+=ni[i];
}
//for(int i=1;i<=n;i++) cout<<sh[i]<<" "<<ni[i]<<endl;
memset(c,,sizeof(c));
for(int i=n;i>=;i--){
add(a[i]);
int tmp=query(a[i]-);
sh[i]+=tmp;
ni[i]+=n-i+-tmp-;
}
//for(int i=1;i<=n;i++) cout<<sh[i]<<" "<<ni[i]<<endl;
int ans=sum;
for(int i=;i<=n;i++){
sum=sum+ni[i]-sh[i];
ans=min(ans,sum);
}
printf("%d\n",ans);
}
return ;
}

HDU1394 逆序数的更多相关文章

  1. hdu1394逆序数(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目大意:逆序数:即假设在数组a中,假如i<j,但是a[i]>a[j]. 现在有一个 ...

  2. 线段树求逆序数方法 HDU1394&amp;&amp;POJ2299

    为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...

  3. 【线段树求逆序数】【HDU1394】Minimum Inversion Number

    题目大意: 随机给你全排列中的一个,但不断的把第一个数丢到最后去,重复N次,形成了N个排列,问你这N个排列中逆序数最小为多少 做法: 逆序数裸的是N^2 利用线段树可以降到NlogN 具体方法是插入一 ...

  4. hdu-1394(线段树&逆序数的性质和求法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目大意: 给出一个序列,一对逆序数就是满足i<j&&a[i]>a[ ...

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

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

  6. 线段树-最小逆序数hdu1394

    title: 线段树-最小逆序数 date: 2018-10-12 17:19:16 tags: acm 算法 刷题 categories: ACM-线段树 概述 这是一道简单的线段树的题,,,当然还 ...

  7. hdu1394 Minimum Inversion Number(最小逆序数)

    Minimum Inversion Number Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/O ...

  8. HDU-1394 Minimum Inversion Number (逆序数,线段树或树状数组)

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

  9. hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)

    题目传送门 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

随机推荐

  1. ELK部署方法

    最近经理开会说公司要安装ELK日志管理让我们搭建ELK,下面是我搭建步骤和流程,用三台机测试机器搭建的. 软件包我都 给你们放/usr/local/src/elk目录下安装目录都放在/usr/loca ...

  2. ajax获取动态列表数据后的分页问题

    ajax获取动态列表数据后的分页问题 这是我在写前台网站时遇到的一个分页问题,由于数据是通过ajax的方式来请求得到的,如果引入相应的js文件来做分页,假如只是静态的填放数据到列表各项内容中(列表条数 ...

  3. Fluent Python: @property

    Fluent Python 9.6节讲到hashable Class, 为了使Vector2d类可散列,有以下条件: (1)实现__hash__方法 (2)实现__eq__方法 (3)让Vector2 ...

  4. Python面向对象-类成员

    类的成员可以分为三大类:字段.方法和属性: 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通字段.而其他的成员,则都是保存在类中,即:无论对象的多少,在 ...

  5. psp1111

    1 本周psp 2.本周进度条 3.本周累积进度图 代码累积折线图 博文字数累积折线图 4.本周PSP饼状图

  6. 11.24Daily Scrum(2)

    人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.996 数据库测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.997 实现视频浏览的功能 王 ...

  7. 项目--uml

    [团队信息] 团队项目: 小葵日记--主打记录与分享模式的日记app 队名:日不落战队 队员信息及贡献分比例: 短学号 名 本次作业博客链接 此次作业任务 贡献分配 备注 501 安琪 http:// ...

  8. 《剑指offer》---两个栈实现队列

    本文算法使用python3实现 1.题目描述:   用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型.   时间限制:1s:空间限制:32768K 2.思路描述:   ...

  9. iOS- Apple零配置网络协议Bonjour的使用?

    1.前言 这段时间为了解决公司App的网络离线需求,做了个Apple推出的零配置网络协议Bonjour的Test,主要是为了解决iOS设备的IP获取,之前是可以使用socket的广播来实现,但是使用A ...

  10. Swift-map()跟flatMap()区别

    map()方法介绍 map() 是  Array 提供的方法,通过接收一个函数作为传入参数,对数组中每个元素进行函数变换得到新的结果值.这样只需要提供  X->Y 的映射关系,就能将数组  [X ...