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. 几个常见移动平台浏览器的User-Agent

    之前介绍的手机站跳转url的一片文稿中提到,依据User Agent判断终端的方法.(文章地址:http://www.cnblogs.com/dereksunok/p/3664169.html ) 若 ...

  2. JDK源码分析:Integer.java部分源码解析

    1)声明部: public final class Integer extends Number implements Comparable<Integer> extends Number ...

  3. Android开发-API指南-<supports-screens>

    <supports-screens> 英文原文:http://developer.android.com/guide/topics/manifest/supports-screens-el ...

  4. javascript对table的添加,删除行的操作

    <body> <form name="myForm"> <table width="100%" id="tab" ...

  5. 安装HIVE

    参考:https://cwiki.apache.org/confluence/display/Hive/GettingStarted 1.下载hive安装包     到apache官网或者其它地方下载 ...

  6. vue学习笔记之:为何data是一个方法

    vue学习笔记之:为何data是一个方法 在vue开发中,我们可以发现,data中的属性值是在function中return出来的.可为何data必须是一个函数呢?我们先看官方的解释: 当一个组件被定 ...

  7. java — 设计模式

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 一.设计模式的分类 ...

  8. Python 零碎信息-基础 01

    1. """ 可以插入多行文字. print """ abC 123' 456''" #单引号, 双引号, 也没有关系 " ...

  9. html中图片自适应浏览器和屏幕,宽度高度自适应

    1.(宽度自适应):在网页代码的头部,加入一行viewport元标签. <meta name="viewport" content="width=device-wi ...

  10. 这些JavaScript编程黑科技,装逼指南,高逼格代码,让你惊叹不已

    Javascript是一门很吊的语言,我可能学了假的JavaScript,哈哈,大家还有什么推荐的,补充送那啥邀请码. 本文秉承着:你看不懂是你SB,我写的代码就要牛逼. 1.单行写一个评级组件 &q ...