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

题意:每次能够将第一个放到最后一个,求这个过程中最小的逆序数和

思路:线段树的点更新,首先读入序列的时候就能够算出一个逆序数,找找比它大的数的个数,

之后将第一个a[0]放到最后一个的时候,每次降低的是a[0],添加的是(n-a[0]-1)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define lson(x) ((x) << 1)
#define rson(x) ((x) << 1 | 1)
using namespace std;
const int MAXN = 5005;
const int ROOT = 1;
const int inf = 0x3f3f3f3f;
// single point updated
struct seg{
int w;
}; struct segment_tree {
seg node[MAXN * 4]; void update(int pos) {
node[pos].w = node[lson(pos)].w + node[rson(pos)].w;
} void build(int l, int r, int pos) {
if (l == r) { node[pos].w = 0; return; }
int m = l + r >> 1;
build(l, m, lson(pos));
build(m + 1, r, rson(pos));
update(pos);
} void modify(int l, int r, int pos, int x, int y) {
if (l == r) { node[pos].w = 1; return; }
int m = l + r >> 1;
if (x <= m) modify(l, m, lson(pos), x, y);
else modify(m + 1, r, rson(pos), x, y);
update(pos);
} int query(int l, int r, int pos, int x, int y) {
if (x <= l && r <= y) return node[pos].w;
int m = l + r >> 1;
int res = 0;
if (x <= m) res += query(l, m, lson(pos), x, y);
if (y > m) res += query(m + 1, r, rson(pos), x, y);
return res;
} int remove(int l, int r, int pos, int x) {
if (l == r) { node[pos].w = 0; return l; }
int m = l + r >> 1;
int res;
if (x < node[lson(pos)].w) res = remove(l, m, lson(pos), x);
else res= remove(m + 1, r, rson(pos), x - node[lson(pos)].w);
update(pos);
return res;
}
} tree; int main() {
int n;
int a[MAXN];
while (scanf("%d", &n) != EOF) {
tree.build(1, n, 1);
int cnt = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
cnt += tree.query(1, n, 1, a[i]+1, n);
tree.modify(1, n, 1, a[i]+1, 1);
}
int ans = cnt;
for (int i = 0; i < n-1; i++) {
cnt += (n - 2*a[i] - 1);
ans = min(ans, cnt);
}
printf("%d\n", ans);
}
return 0;
}

HDU - 1394 Minimum Inversion Number (线段树求逆序数)的更多相关文章

  1. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

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

  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 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...

  4. HDU 1394 Minimum Inversion Number 线段树

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...

  5. HDU 1394 Minimum Inversion Number(线段树 或 树状数组)

    题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...

  6. HDU_1394_Minimum Inversion Number_线段树求逆序数

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

  7. hdu 13394 Minimum Inversion Number 线段树

    题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...

  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. HDU 1394 Minimum Inversion Number (树状数组)

    题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...

  10. HDU 1394 Minimum Inversion Number(树状数组/归并排序实现

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

随机推荐

  1. Java线程Dump分析工具--jstack(转)

    jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack使 ...

  2. Flex发行2048游戏

    近来的2048像挺火的游戏.在公交车,吃.甚至还有人走在路上拿着手机在玩.之前我看我的同事们戏,我认为这是很天真,中移动太无聊了吧 到后面,他是在,我觉得真的很无聊,这时候,无聊的时候无聊,后来我想用 ...

  3. 返璞归真 asp.net mvc (8) - asp.net mvc 3.0 新特性之 Model

    原文:返璞归真 asp.net mvc (8) - asp.net mvc 3.0 新特性之 Model [索引页][源码下载] 返璞归真 asp.net mvc (8) - asp.net mvc ...

  4. 构建安全的Xml Web Service系列之初探使用Soap头

    原文:构建安全的Xml Web Service系列之初探使用Soap头 Xml Web Service 从诞生那天就说自己都么都么好,还津津乐道的说internet也会因此而进入一个新纪元,可5年多来 ...

  5. Android设计模式(十)--生成器模式

    回头看自己写的东西,大概Android当自己控制的定义,编写代码适用性比较高.但是,看看没有什么技术含量,因此,当在学习设计模式,想想有些东西是否可以改善,例如: 自己定义Dialog是Android ...

  6. CentOS6.5设备MRBS

    //--------------------------------------软件必须安装-----------------------------------// # yum install –y ...

  7. Cocos2d-X 使用CCTableView创建滚动视图

    CCTableView和CCScrollView如创建滚动视图,CCTableView该函数将是更,制造更多麻烦 实例1:使用CCTableView创建滚动视图 首先创建一个TableView类 Ta ...

  8. Windows在生产体系Android开关机动画

    在Windows根据系统.办Android开关机动画,几个需要注意的问题: 1.压缩的选择 2.压缩的格式: 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  9. Paypal-Express Checkout快捷支付方式的android端开发心得(二)

    一.前导 上一篇讲的不是非常好,这里再又一次讲一下. Paypal手机支付有2种形式: 1.Mobile Express Checkout,MEC,快捷支付 2.MPL 假设採用MEC支付方式,这样的 ...

  10. 数据库管理——安全管理——识别SQLServer中空密码或者弱密码的登录名

    原文:数据库管理--安全管理--识别SQLServer中空密码或者弱密码的登录名 原文译自: http://www.mssqltips.com/sqlservertip/2775/identify-b ...