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. RH033读书笔记(5)-Lab 6 Exploring the Bash Shell

    Lab 6 Exploring the Bash Shell Sequence 1: Directory and file organization 1. Log in as user student ...

  2. arch linux设备(请参考官方文档,桌面安装没有找到一个好工作后)

    首先,启动安装系统(一获得通过vmware虚拟机) 1.设置键盘布局 #loadkeys "us" #设置为美国的键盘布局.一般能够默认就可以 2.建立硬盘的分区 我採用的是fdi ...

  3. android AlarmManager采用

    Android的闹钟实现机制非常easy, 仅仅须要调用AlarmManager.Set()方法将闹钟设置提交给系统,当闹钟时间到后,系统会依照我们的设定发送指定的广播消息.我们写一个广播去接收消息做 ...

  4. jQuery 操作 input 之 checkbox

    jQuery 操作 input 之 checkbox 一片 HTML 清单: <input type="checkbox" name="hobby" va ...

  5. 返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性

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

  6. canvas绘制百分比圆环进度条

    开发项目,PM会跟踪项目进度:完成某个事情,也可以设置一个完成的进度. 这里用canvas绘制一个简单百分比圆环进度条. 看下效果: 1. 动画方式   2. 静默方式   // 贴上代码,仅供参考 ...

  7. Windows Auzre 微软的云计算产品的后台操作界面

    Windows Auzre 微软的云计算产品的后台操作界面,试用期,相比于阿里云后台操作不是人. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTmFvbG ...

  8. java得到clientIP地址和MAC住址

    最近的项目应该得到client的mac住址. 服务器移植centos制,arm建筑箱.client手机和移动设备.(其他方案也应该是一流的似的) 首先,要获得ip住址: 依据client的http请求 ...

  9. 一个轻量级rest服务器

    RestServer直接发布数据库为json格式提供方法 RestSerRestServer直接发布数据库为json格式 支持MySQL,SqlServer,Oracle直接发布为Rest服务, 返回 ...

  10. ASP.NET Core官方资料入口

    ASP.NET 5 has been renamed to ASP.NET Core 1.0 传送门