Minimum Inversion Number

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

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
 

逆序数的概念: 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个 逆序 。一个排列中逆序的总数就称为这个排列的 逆序数 。逆序数为 偶数 的排列称为 偶排列 ;逆序数为奇数的排列称为 奇排列 。如2431中,21,43,41,31是逆序,逆序数是4,为偶排列。

逆序数计算方法是:在逐个元素读取原始数列时,每次读取都要查询当前已读取元素中大于当前元素的个数,加到sum里,最后得到的sum即为原始数列的逆序数。

接下来找数列平移后得到的最小逆序数,假设当前序列逆序数是sum,那么将a[0]移到尾部后逆序数的改变是之前比a[0]大的数全部与尾部a[0]组合成逆序数,假设数量为x,则x=n-1-a[0],而之前比a[0]小的数(也就是之前能和a[0]组合为逆序数的元素)不再与a[0]组合成逆序数,假设数量为y,则y=n-x-1,这样,新序列的逆序数就是sum+x-y=sum-2*a[0]+n-1;

接下来说明下线段树的作用,线段区间表示当前已读取的元素个数,比如[m,n]表示在数字m到n之间有多少个数已经读入,build时所有树节点全部为0就是因为尚未读数,ins函数是将新读入的数字更新到线段树里,点更新,query函数是查询当前数字区间已存在的数字个数。

除去逆序数方面的内容,这基本就是一道线段树的模板题

//2016.8.10
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 5005
#define lson (id<<1)
#define rson ((id<<1)|1)
#define mid ((l+r)>>1) using namespace std; struct node
{
int sum;
}tree[N*]; int arr[N]; void build_tree(int id, int l, int r)
{
tree[id].sum = ;
if(l == r){
return ;
}
build_tree(lson, l, mid);
build_tree(rson, mid+, r);
return;
} void ins(int id, int l, int r, int pos)
{
if(l == r){
tree[id].sum = ;return ;
}
if(pos <= mid)
ins(lson, l, mid, pos);
else
ins(rson, mid+, r, pos);
tree[id].sum = tree[lson].sum + tree[rson].sum;
return ;
} int query(int id, int l, int r, int ql, int qr)
{
if(ql<=l&&r<=qr){
return tree[id].sum;
}
int cnt = ;
if(ql<=mid)cnt += query(lson, l, mid, ql, qr);
if(mid+<=qr)cnt += query(rson, mid+, r, ql, qr); return cnt;
} int main()
{
int n;
while(cin>>n)
{
int sum = ;
build_tree(, , n-);
for(int i = ; i < n; i++){
scanf("%d", &arr[i]);
sum+=query(, , n-, arr[i], n-);
ins(, , n-, arr[i]);
}
//得到初始序列的逆序数
int ans = sum;
for(int i = ; i < n; i++)//移动数列找最小逆序数
{
sum = sum-*arr[i]+n-;
if(ans > sum)ans = sum;
}
cout<<ans<<endl;
} return ;
}

树状数组

 //2016.9.13
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 5005 using namespace std; int a[N], arr[N], n; int lowbit(int x){return x&(-x);} void add(int pos, int tt)
{
for(int i = pos; i <= n; i+=lowbit(i))
arr[i] += tt;
} int query(int pos)
{
int sum = ;
for(int i = pos; i > ; i-=lowbit(i))
sum += arr[i];
return sum;
} int main()
{
int sum, ans, tmp;
while(scanf("%d", &n)!=EOF)
{
memset(arr, , sizeof(arr));
for(int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
a[i];
}
sum = ;
for(int i = ; i <= n; i++)
{
sum += query(n-a[i]);//n-a[i]表示a[i]为第n-a[i]大
add(n-a[i], );
}
ans = sum;
for(int i = ; i < n; i++)
{
add(n-a[i], -);
sum = sum+query(n-a[i])-a[i];
add(n-a[i], );
if(ans > sum) ans = sum;
}
printf("%d\n", ans);
} return ;
}

HDU1394(线段树||树状数组)的更多相关文章

  1. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

  2. hdu1394(枚举/树状数组/线段树单点更新&区间求和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...

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

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

  4. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

  5. [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  6. HDU 1556 线段树或树状数组,插段求点

    1.HDU 1556  Color the ball   区间更新,单点查询 2.题意:n个气球,每次给(a,b)区间的气球涂一次色,问最后每个气球各涂了几次. (1)树状数组 总结:树状数组是一个查 ...

  7. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  8. 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings

    谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...

  9. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

随机推荐

  1. Blog开始

    好久没更新Blog了,去看了下之前的csdn的blog感觉特别的乱,为此决心重开blog,记录工作及学习中的一些事 2013-10-28 ymc ...

  2. tp框架实现文件上传

    public function shangchuan() { $this->display(); } public function upload() { $uplode= new \Think ...

  3. 基于LNMP的Zabbbix之Zabbix Agent源码详细安装,但不给图

    基于LNMP的Zabbbix之Zabbix Server源码详细安装:http://www.cnblogs.com/losbyday/p/5828547.html wget http://jaist. ...

  4. [iOS]使用signal让app能够在从容崩溃

    前言 虽然大家都不愿意看到程序崩溃,但可能崩溃是每个应用必须面对的现实,既然崩溃已经发生,无法阻挡了,那我们就让它崩也崩得淡定点吧. iOS SDK中提供了一个现成的函数 NSSetUncaughtE ...

  5. list集合怎么转化成一个javaBean对象,及常见的使用方法(全)

    一.List集合的用法 1.list集合添加实体并输出 for (int i = 0; i < list.size(); i++) { javabean obj= (javabean)list. ...

  6. 布隆过滤器(BoomFilter)

    1.原理:           a.解决的问题:                判断一个元素是否在一个集合中             b.Hash表的特点:                i.快速准确 ...

  7. codeforces #321 DIV2

    A题: 链接:http://codeforces.com/contest/580/problem/A dp,最长连续不上升子序列 #include<iostream> #include&l ...

  8. APP导致界面卡死,iPhone卡死

    实测,是 Reachability 类创建实例过多导致 http://stackoverflow.com/questions/34063166/ios-9-app-freeze-with-consol ...

  9. 在线文档转换API word,excel,ppt等在线文件转pdf、png

    在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...

  10. 【转】高性能服务器架构(High-Performance Server Architecture)

    High-Performance Server Architecture 高性能服务器架构 来源:http://pl.atyp.us/content/tech/servers.html译文来源:htt ...