博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Minimum Inversion Number(线段树求逆序数)
阅读量:4037 次
发布时间:2019-05-24

本文共 2927 字,大约阅读时间需要 9 分钟。

有一点没想明白,今天突然间想明白了,sum[rt]记录的是rt表示的区间有几个数,而输入一个数,查询一次,查询的目的就是返回当前区间内已经输入了多少个数,查询区间是(x,n-1)是因为要找总共有多少个逆序数,比如说先输入5,再输入3,那么输入3时查询前边输入的数有几个比三大的即当前这些数的总的逆序数,因为输入3,前边找到一个比3大的数,那么就看5,3,可以知道,5由于3的输入,逆序数加1,所以有用

ans += query(x[i] , n - 1 , 0 , n - 1 , 1);此时ans里边记录的就是当前序列的逆序数之和

 

1、题目大意:

给定一串数字,求这一组数字的逆序数,而且这组数据可以改变,一次将前边的第一个数移到最后一个数的位置,构成新的数列,在诸多序列中,求出一个最小的逆序数

2、思路:

简单的线段树处理,单点更新即可,网上有一个很巧妙的处理最小逆序数的方法,

当把x放入数组的后面,此时的逆序数应该为x没放入最后面之前的逆序总数加上(n-x)再减去(x-1);sum = sum+(n-x[i])-(x[i]-1)。

此方法现在才明白,例如3 1 4 2 5,如果将3移到最后,那么比3大的数4,5的逆序数分别加1,即上式中的加上(n-x),而比3小的1,2的逆序数相比较以前分减1,即上式中的再减(x-1)

3、题目:

Minimum Inversion NumberTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5831    Accepted Submission(s): 3547Problem DescriptionThe 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. InputThe 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. OutputFor each case, output the minimum inversion number on a single line. Sample Input101 3 6 9 0 8 5 7 4 2 Sample Output16 AuthorCHEN, Gaoli SourceZOJ Monthly, January 2003  RecommendIgnatius.L

 

4、代码:

#include
#include
#include
using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int x[5005];int sum[5005*4];void build(int l,int r,int rt){ sum[rt]=0; if(l==r) return ; int m=(l+r)>>1; build(lson); build(rson);}int query(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r) return sum[rt]; int m=(l+r)>>1,ret=0; if(L<=m) ret+=query(L,R,lson); if(R>=m+1) ret+=query(L,R,rson); return ret;}void update(int x,int l,int r,int rt){ if(l==r) { sum[rt]++; return ; } int m=(l+r)>>1; if(x<=m) update(x,lson); if(x>=m+1) update(x,rson); sum[rt]=sum[rt<<1]+sum[rt<<1|1];}int main(){ int n,ans; while(scanf("%d",&n)!=EOF) { ans=0; build(0,n-1,1); for(int i=1; i<=n; i++) { scanf("%d",&x[i]); ans+=query(x[i],n-1,0,n-1,1); update(x[i],0,n-1,1); } int minsum=ans; for(int i=1; i<=n; i++) { ans=ans+(n-x[i])-(x[i]+1); minsum=min(ans,minsum); } printf("%d\n",minsum); } return 0;}/*101 3 6 9 0 8 5 7 4 2*/

 

转载地址:http://yfcdi.baihongyu.com/

你可能感兴趣的文章
【leetcode】Word Break(python)
查看>>
【剑指offer】面试题26:复杂链表的复制
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
windows下 pip安装
查看>>
线程和进程
查看>>
UNIX 学习笔记-文件I/O(open)
查看>>
UNIX学习笔记-文件I/O--(creat,close)
查看>>
UNIX学习笔记--(lseek)
查看>>
java swing最简单实例(1) 一个空的JFrame
查看>>
java swing最简单实例(2) 往JFrame里面放一个容器或组件
查看>>
java自定义容器排序的两种方法
查看>>
进化论中的概率论 进化13个字母序列的无差期望值公式
查看>>
网络英语 第一章
查看>>
搞笑短信用英文写起来啥样呢……
查看>>
The Relationship Between ZB,NB,SB and LP 第一篇
查看>>
百度vs 谷歌(本来大家都叫它“狗狗”,居然叫谷歌,名字够难听)
查看>>
如何成为编程高手
查看>>