POJ 2299 树状数组+离散化求逆序对】的更多相关文章

给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数相加 因为只有后面的数比自己笑才能交换 但是暴力求逆序数也会超时 于是用树状数组求 从最后往前看 每次求sum相加 但是数据的范围根本开不出来树状数组... 但是由于n最多只有五十万 所以把它离散化一下 把这n个数变成1~n 技巧就是先按大小排序 然后把他们变成1~n 然后按照结构体中的顺序再排回来…
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 9 1 0 5…
对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们通过树状数组可以明显提高时间效率. 我们可以按照排列的顺序依次将数字放入树状数组中,并依次更新预与之相关联的树状数组元素.那么在将其更新完毕后,我们知道每个数对应的树状数组元素的左边的数肯定比它小,我们在以序列顺序依次更新树状数组时,如果有值在它前面出现,那么它对应的树状数组元素(在这个题目里存放的…
http://codeforces.com/problemset/problem/61/E 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 会树状数组求逆序数的话,这个推一下就能出结果: 做法: 1.离散化,由于a[i]能够达到1e9 2.插入a[i]的时候,记录x[i]=i-sum(a[i]); a[i]之前比a[i]大的有x[i]个 3.插入完毕后,求a[i] 之后比a[i]小的数的个数y[i] ans=segma(x[i]*y[i])   注…
Time Limit: 1000MS Memory Limit: 65536K Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <=…
题目链接 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input…
我好咸鱼. 归并排序之前写过,树状数组就是维护从后往前插入,找比现在插入的数大的数的数量. 如果值域大,可以离散化 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N=100005; int a[N],c[N],cnt,t[N],n,ans; void modify(int x) { for(int i=x; i<=n; i+=i&am…
Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 54883   Accepted: 20184 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin…
http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num[]的大小间接排序 2.bs[as[i]]=i:如今bs数组就是num[]数组的离散化后的结果 3.注意,树状数组中lowbit(i)  i是不能够为0的,0&(-0)=0,死循环... #include <cstdio> #include <cstring> #include…
求逆序对最常用的方法就是树状数组了,确实,树状数组是非常优秀的一种算法.在做POJ2299时,接触到了这个算法,理解起来还是有一定难度的,那么下面我就总结一下思路: 首先:因为题目中a[i]可以到999,999,999之多,在运用树状数组操作的时候,用到的树状数组C[i]是建立在一个有点像位存储的数组的基础之上的,不是单纯的建立在输入数组之上. 比如输入一个9 1 0 5 4(最大9) 那么C[i]树状数组的建立是在: 下标 0 1 2 3 4 5 6 7 8 9 –——下标就要建立到9 数组…