Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 19575    Accepted Submission(s): 11756

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个逆序数,从这里面选取最小的一个。

思路:

1.暴力

将初始序列的逆序数求出来,之后的逆序数可以推导出来,假设得到了第一个逆序数 num。此时要将第一个数字data[i]放置到最后一个,那么num就要减去该数的值,重新产生了多少个逆序对呢?产生了n-data[i]-1个。就是序列中比data[i]大的数的个数。这个很容易推导出来。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=50005;
int ans[maxn];
int main() {
int n;
while(~scanf("%d",&n)) {
for(int i=0;i<n;++i) {
scanf("%d",&ans[i]);
}
int sum=0;
for(int i=0;i<n-1;++i) {
for(int j=i+1;j<=n-1;++j) {
if(ans[i]>ans[j]) sum++;
}
}
int result=sum;
for(int i=0;i<n-1;++i) {
sum=sum-ans[i]+(n-ans[i]-1);
result=min(result,sum);
}
printf("%d\n",result);
}
return 0;
}

2.用线段树来做。

有了上面的推导,只要求出第一个,剩余的工作就变的简单了。

再用线段树求原始序列的时候要反向思维,求每一个数之前的序列中比自身大的个数。

假设现在给出一个序列是 : 4 3 1 5 0 2

我们在一边度数据的过程中计算逆序对的个数。初始话每个线段树的结点值都赋值为0。

先读到5,查询4-(n-1)区间即4-4有没有出现比4大的,没有,返回0。查询完成之后将该节点插入线段树。

再读到3,查询3-(n-1)区间有没有比3大的,返回1,将3插入。再将3插入线段树。以此类推...这样我们得到如下结果:

4  :  0

3  :  1(4)

1  :  2(3,4)

5  :  0

0  :  4(1,3,4,5)

2  :  3(3,4,5)

该序列的逆序数就是0+1+2+0+4+3=10。与我们求一般求逆序数时得到的结果相同。



注意是0-n-1的序列,建树注意下标



代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=5005;
int sum[maxn<<2];
int data[maxn];
void pushup(int rt) {
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l, int r, int rt) {
sum[rt]=0;
if(l==r)return;
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int val, int l, int r, int rt) {
if(l==r) {
sum[rt]++;return;
}
int mid=(l+r)>>1;
if(val<=mid) update(val,lson);
else update(val,rson);
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)>>1,cnt=0;
if(L<=mid) cnt+=query(L,R,lson);
if(R>mid) cnt+=query(L,R,rson);
return cnt;
}
int main() {
int n;
while(~scanf("%d",&n)) {
int result,temp=0;
build(0,n-1,1);
for(int i=0;i<n;++i) {
scanf("%d",&data[i]);
temp+=query(data[i],n-1,0,n-1,1);
update(data[i],0,n-1,1);
}
result=temp;
for(int i=0;i<n;++i) {
temp=temp-data[i]+(n-1-data[i]);
result=min(result,temp);
}
printf("%d\n",result);
}
return 0;
}

HDU 1394 逆序数 线段树单点跟新 | 暴力的更多相关文章

  1. HDU 1394 Minimum Inversion Number(最小逆序数 线段树)

    Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...

  2. HDU 1754 线段树 单点跟新 HDU 1166 敌兵布阵 线段树 区间求和

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. hdu 1394 逆序数(线段树)

    http://acm.hust.edu.cn/vjudge/problem/15764 http://blog.csdn.net/libin56842/article/details/8531117 ...

  4. HDU 1394 (逆序数) Minimum Inversion Number

    原来求逆序数还可以用线段树,涨姿势了. 首先求出原始序列的逆序数,然后递推每一个序列的逆序数. #include <cstdio> #include <cstring> #in ...

  5. hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

    Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  6. hdu 1541 Stars(线段树单点更新,区间查询)

    题意:求坐标0到x间的点的个数 思路:线段树,主要是转化,根据题意的输入顺序,保证了等级的升序,可以直接求出和即当前等级的点的个数,然后在把这个点加入即可. 注意:线段树下标从1开始,所以把所有的x加 ...

  7. 51Nod 1019 逆序数(线段树)

    题目链接:逆序数 模板题. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a) ...

  8. hdu 2795 Billboard(线段树单点更新)

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. POJ 2299 Ultra-QuickSort 求逆序数 线段树或树状数组 离散化

    我用的线段树写的. num数组表示已插入的数值的个数. 由于a[i]数值很大,但是n不是很大,所以要离散化处理 9 1 0 5 4 离散化后 4 1 0 3 2 这样保证最大值不会超过n #inclu ...

随机推荐

  1. tamcat的使用

    tomcat的基础知识 一.tomcat的定义 apache的官网是这么说的:使用Apache Tomcat ®软件了Java Servlet,JavaServer页,Java表达式语言和Java的W ...

  2. ABAP开发实用快捷键

    在程序中注释代码往往受输入法影响,看了别人的一篇博客,结合自己的测试发现用如下方法可以直接注释源代码不受输入法影响 添加注释:ctrl + space + < 去掉注释:ctrl + space ...

  3. js数组遍历some,foreach,map,filter,every对比

    1.  [...].some(ck)函数       ----      某个一个为true,则为true 对数组中每个元素执行一次ck函数,知道某个元素返回true,则直接返回true.如果都返回f ...

  4. MSBuild Tools解决办法

    每次在CI上通过Msbuild做发布,基本都会碰到下面的问题 error MSB4019: 未找到导入的项目"C:\Program Files (x86)\MSBuild\Microsoft ...

  5. 服务器 : Apache Tomcat - 理解架构层次

    文章概览 相信很多接触java的人都对Tom猫有着多少的熟悉,就个人而言,本来只知道Tom简单的操作与配置,就像裹上一层纱,迷迷糊糊的. Tomcat的书籍本来就不多,高分的还是很久之前的版本,直到最 ...

  6. scala读取parquet文件

    import org.apache.spark.SparkConfimport org.apache.spark.SparkContextimport org.apache.spark.sql.SQL ...

  7. vue 2 仿IOS 滚轮选择器 从入门到精通 (一)

    大家好,由于最近从事的是微信公众号和APP内嵌 H5开发,避免不了开发一些和native相同的操作功能,就如接下来说的 仿IOS滚轮选择器. 先来个截图: 接下来具体介绍如何实现的.能力有限避免不了错 ...

  8. 两个Xml转换为DataSet方法(C#)

    ///通过传入的特定XML字符串,通过 ReadXml函数读取到DataSet中.protected static DataSet GetDataSetByXml(string xmlData){   ...

  9. 《天书夜读:从汇编语言到windows内核编程》七 内核字符串与内存

    1)驱动中的字符串使用如下结构: typedef struct _UNICODE_STRING{ USHORT Length; //字符串的长度(字节数) USHORT MaximumLength; ...

  10. [转载] Java集合---HashMap源码剖析

    转载自http://www.cnblogs.com/ITtangtang/p/3948406.html 一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供所有可选的映射 ...