Minimum Inversion Number

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

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

题意:一个长度为n 的 0-n-1 的全排列  你可以进行若干次操作 把第一个数放到最后面 形成一个新的序列(最多n个)问所有序列中逆序对最少的数量。

解析:逆序对很好求 对于每一个数看它前面有多少个比它大的记为sum[ a[i] ] sigmasum[] 就是逆序的数量 每个位置都如此,线段树就是每次查询[a[i],n-1] 的个数;

假如当前序列的逆序对是ans 那么把当前第一个数a[i] 放到最后面 后面对于每个数[0,a[i]) sum[ ] 就会少一次 同样 sum[ a[i] ] = n-1-a[i]

所以 操作之后就是 ans+=n-a[i]+n-1-a[i]。取最大值就好了

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n")
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl
#define ffread(a) fastIO::read(a)
using namespace std;
typedef long long ll;
const int maxn = 1e4+;
const int inf = 0x3f3f3f3f;
const int mod = ;
const double epx = 1e-;
const double pi = acos(-1.0);
//head-----------------------------------------------------------------
int sum[maxn*];
void PushUp(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=;
return;
}
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
PushUp(rt);
}
void update(int x,int val,int l,int r,int rt)
{
if(l==x&&r==x)
{
sum[rt]+=val;
return;
}
int mid=(l+r)>>;
if(x<=mid)
update(x,val,l,mid,rt<<);
else
update(x,val,mid+,r,rt<<|);
PushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
return sum[rt];
}
int mid=(l+r)>>;
int ans=;
if(L<=mid)
ans+=query(L,R,l,mid,rt<<);
if(R>mid)
ans+=query(L,R,mid+,r,rt<<|);
return ans;
}
int a[maxn];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
fillchar(sum,);
int ans=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
update(a[i]+,,,n,); //出于方便从1开始
ans+=query(a[i]+,n,,n,);
}
int ret=inf;
for(int i=;i<=n;i++)
{
ans+=n-*a[i]-;
ret=min(ans,ret);
}
printf("%d\n",ret);
}
}

复习一下线段树基础。。

HDU 1394 线段树求逆序对的更多相关文章

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

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

  2. 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)

    题目链接:http://codevs.cn/problem/4163/ 题目:

  3. BNU 2418 Ultra-QuickSort (线段树求逆序对)

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...

  4. HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...

  5. hdu1394(线段树求逆序对)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 线段树功能:update:单点增减 query:区间求和 分析:如果是0到n-1的排列,那么如果 ...

  6. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

  7. poj2299 Ultra-QuickSort(线段树求逆序对)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

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

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

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

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

随机推荐

  1. WINDOWS-基础:WINDOWS常用API

    1.窗口信息 //MS 为我们提供了打开特定桌面和枚举桌面窗口的函数. hDesk=OpenDesktop(lpszDesktop,,FALSE,DESKTOP_ENUMERATE); //打开我们默 ...

  2. Java简答题附答案

    1. Java有没有goto? 有, Goto语句在java中作为保留字, 并没有实现它. 带标号的break, continue局限于循环体中跳转 带标号的goto可以在一个函数(c语言)中任意跳转 ...

  3. Hibernate中get()与load()的区别,以及关于ThreadLocal的使用方法

    一.get方法和load方法的简易理解 (1)get()方法直接返回实体类,如果查不到数据则返回null.load()会返回一个实体代理对象(当前这个对象可以自动转化为实体对象),但当代理对象被调用时 ...

  4. 【离线做法 树状数组】luoguP1972 [SDOI2009]HH的项链

    与bzoj3585: mex的线段树做法有着异曲同工之妙 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含 ...

  5. Spring Boot -- Idea搭建下搭建web项目

    最近公司准备使用Spring Boot框架,让小瑾先来学习一下,为了表示小瑾的办事效率,小瑾直接先学习用Idea搭建一个Spring Boot项目,哈哈哈,坐等领导夸. 废话不多说了,先来总结一下用I ...

  6. css兼容处理-hack

    浏览器兼容之旅的第二站:各浏览器的Hack写法 Browser CSS Hacks Moving IE specific CSS into @media blocks Detecting browse ...

  7. Python基础:列表(list)和元组(tuple)

    学一门语言,可以用对比其他语言方法加深对这门语言特点的理解. 一.定义:列表和元组,都是一个可以放置任意数据类型的有序集合. mutable的列表:动态的,可以改变元素 immutable的元组:静态 ...

  8. (转)python之禅

    凡是用过 Python的人,基本上都知道在交互式解释器中输入 import this 就会显示 Tim Peters 的 The Zen of Python,但它那偈语般的语句有点令人费解,所以我想分 ...

  9. POJ:2753-Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Description The little cat is s ...

  10. nw335 debian sid x86-64 -- 5 使用xp的驱动

    nw335 debian sid x86-64 -- 5 使用xp的驱动