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. Json转model对象,model转json,解析json字符串

    GitHub链接: https://github.com/mozhenhau/D3Json D3Json 通过swift的反射特性,把json数据转换为model对象,本类最主要是解决了其他一般jso ...

  2. itchat 微信的使用

    #coding=utf8 import itchat # 自动回复 # 封装好的装饰器,当接收到的消息是Text,即文字消息 @itchat.msg_register('Text') def text ...

  3. Java ftp 上传文件和下载文件

    今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...

  4. python全栈开发-Day13 内置函数

    一.内置函数 注意:内置函数id()可以返回一个对象的身份,返回值为整数. 这个整数通常对应与该对象在内存中的位置,但这与python的具体实现有关,不应该作为对身份的定义,即不够精准,最精准的还是以 ...

  5. mongodb监控工具mongostat

    mongostat的使用及命令详解 mongostat是mongodb自带的状态检测工具,在命令行下使用,会间隔固定时间获取mongodb的当前运行状态,并输出. 1.常用命令格式: mongosta ...

  6. JAVA_SE基础——65.StringBuffer类 ②

    字符串特点:字符串是常量:它们的值在创建之后不能更改.    字符串的内容一旦发生了变化,那么马上会创建一个新 的对象.    注意: 字符串的内容不适宜频繁修改,因为一旦修改马上就会创建一个新的对象 ...

  7. 学习phalcon框架按照官网手册搭建第一个项目注册功能

    中文手册官网:http://phalcon.ipanta.com/1.3/tutorial.html#bootstrap 官网提供http://www.tutorial.com项目源码github地址 ...

  8. New UWP Community Toolkit - DeveloperTools

    概述 UWP Community Toolkit  中有一个开发者工具集 DeveloperTools,可以帮助开发者在开发过程中进行 UI 和功能的调试,本篇我们结合代码详细讲解  Develope ...

  9. 阿里云API网关(18)请求报文和响应报文

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  10. javascript学习(2)修改html元素和提示对话框

    一.修改html元素 1.修改p元素 1.1.源代码 1.2.执行前 1.3.执行后 2.修改div元素的className 2.1.源代码 1.2.执行前 1.3.执行后 3.直接在当前位置输出内容 ...