Minimum Inversion Number

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

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
/*
hdu 1394 求a[i]前面小于a[i]的数的个数
给你一个序列,求每个a[i]后面小于a[i]的个数,
然后你可以把第一个数放到最后,这样的话sum变化:sum = sum+(n-1-a[i])-a[i];
hhh-2016-02-27 15:18:09
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <vector>
typedef long long ll;
using namespace std;
const int maxn = 200000+5;
int a[maxn];
struct node
{
int l,r;
int num;
} tree[maxn<<2]; void push_up(int r)
{
int lson = r<<1,rson = (r<<1)|1;
tree[r].num = (tree[lson].num+tree[rson].num);
}
void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].num = 0;
if(l == r)
{
return ;
}
int mid = (l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
push_up(i);
}
void push_down(int r)
{ } void Insert(int i,int k)
{
if(tree[i].l == k && tree[i].r == k)
{
tree[i].num++;
return ;
}
push_down(i);
int mid = (tree[i].l + tree[i].r) >>1;
if(k <= mid) Insert(i<<1,k);
if(k > mid) Insert(i<<1|1,k);
push_up(i);
} int query(int i,int l,int r)
{
if(tree[i].l >= l && tree[i].r <= r)
{
return tree[i].num;
}
push_down(i);
int mid = (tree[i].l+tree[i].r)>>1;
int ans = 0;
if(l <= mid) ans+=(query(i<<1,l,r));
if(r > mid) ans+=(query(i<<1|1,l,r));
return ans ;
} int main()
{
int T,n,m,cas = 1;
while(scanf("%d",&n)!=EOF)
{
build(1,0,n-1);
int sum = 0;
for(int i =1; i <= n; i++)
{
scanf("%d",&a[i]);
Insert(1,a[i]);
int t;
if(a[i] - 1 < 0)
t = 0;
else
t = query(1,0,a[i]-1);
sum += (a[i] - t);
} int ans = sum;
for(int i = 1;i <= n;i++)
{
sum = sum+(n-1-a[i])-a[i];
ans = min(ans,sum);
}
printf("%d\n",ans);
}
return 0;
}

  

hdu 1394 线段树的更多相关文章

  1. hdu 1394(线段树) 最小逆序数

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 给出一列数组,数组里的数都是从0到n-1的,在依次把第一个数放到最后一位的过程中求最小的逆序数 线段树的应 ...

  2. hdu 1394 (线段树求逆序数)

    <题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...

  3. HDU 1394 线段树求逆序对

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

  4. HDU 1394 线段树or 树状数组~

    Minimum Inversion Number Description The inversion number of a given number sequence a1, a2, ..., an ...

  5. hdu 1394 线段树计算逆序数

    线段树计算逆序数的原理: 用线段树来统计已插入的数的个数(所以要保证最大的那个数不能太大,否则数组都开不了),然后每插入一个数,就查询比插入的数大的个数,累加即可. 这个题还有一个特点就是,题目给的是 ...

  6. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  7. hdu 3974 线段树 将树弄到区间上

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdu 3436 线段树 一顿操作

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  9. hdu 3397 线段树双标记

    Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. Tornado 网站demo 三

    模板 修改index.py #!/usr/bin/env Python # coding=utf-8 import tornado.web import methods.readdb as mrd c ...

  2. 怎么去理解JAVA中类与对象的关系

    首先要明确,在现实生活中,每一个物体都有自己的基本特征,专业一点也可以说成是属性有些甚至还有一定的行为.例如 汽车的特征:有车门.有轮胎.颜色各一等等,行为:有行驶,开车门,开车灯,等等.有这些属性和 ...

  3. EasyUI 主布局整合。

    博文学习地址:http://www.cnblogs.com/xishuai/p/3620327.html html: <%@ Page Language="C#" AutoE ...

  4. Java中Math类的常用方法

    public class MathDemo { public static void main(String args[]){ /** * abs求绝对值 */ System.out.println( ...

  5. python端口扫描用多线程+线程安全的队列+Thread类实现

    用线程安全的队列Queue实现扫描端口数据存储 用多线程扫描端口 用Thread类实现程序组织 #coding:utf-8 import sys import socket import sys im ...

  6. java 细说String

    String类内部用一个字符数组表示字符串,实例变量定义为: private final char value[]; String有两个构造方法,可以根据char数组创建String public S ...

  7. Spring中报"Could not resolve placeholder"的解决方案

    除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceholderCon ...

  8. Spring Security 入门(1-8)Spring Security 的配置文件举例

  9. SpringMVC架构的项目,js,css等静态文件导入有问题

    发生原因 <servlet> <servlet-name>springmvc-mybaits</servlet-name> <servlet-class> ...

  10. python 类的进阶

    类的进阶 一 isinstance(obj,cls)和issubclass(sub,super) class Foo: def __init__(self,name): self.name = nam ...