HDU 2852 (树状数组+无序第K小)】的更多相关文章

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小数. 解题思路: 由于模型是盒子,而不是序列,所以可以用树状数组的顺序维护+逆序数思想. 对应的树状数组Solution: 放一个数 $Add(val,1)$ 类似维护逆序数的方法,对应位置上计数+1. 注意Add的while范围要写成$while(x<maxn)$ 如果范围不是最大,那么会导致某些…
Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data…
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般取lg(MAX_VAL) { ans += (1 << i); if (ans >= maxn || cnt + c[ans] >= k) ans -= (1 << i); else cnt += c[ans]; } return ans + 1 } 首先树状数组c[i]里…
题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? pid=2852 题意:spoj227:告诉每一个位置前面有多少个数比当前位置小,求出原序列. hdu2852:设计一个容器,支持几种操作:添加/删除元素,求容器中比a大的数中第k小的数是多少. 分析:两个题思路都是求数组里面的第K小的数.開始一直在找O(N*logN)的方法,后来发现O(N*logN*lo…
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中a[i]为第i位是i往右中的数里 第几大的-1(比他小的有几个). 其实直接想也可以,有点类似数位DP的思想,a[n]*(n-1)!也就是a[n]个n-1的全排列,都比他小 一些例子 http://www.cnblogs.com/hxsyl…
KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3864    Accepted Submission(s): 1715 Problem Description For the k-th number, we all should be very familiar with it. Of course,to…
思路:加一个数e就用update(e,1).删除元素e就用update(e,-1).找比a大的第k大的元素就用二分查找. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define Maxn 120010 #define lowbit(x) (x&(-x)) using namespace std; int C[Maxn]; int Sum(int…
#include<iostream> #include<cstring> using namespace std; ; int tr[N]; int lowbit(int x) { return x&(-x); } void add(int x,int a) { for(int i=x;i<N;i+=lowbit(i)) tr[i]+= a; } int sum(int x) { ; for(int i=x;i;i-=lowbit(i)) ans+=tr[i]; re…
The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted: 2712 Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to g…
原帖:http://www.cnblogs.com/zgmf_x20a/archive/2008/11/15/1334109.html 回顾树状数组的定义,注意到有如下两条性质: 一,c[ans]=sum of A[ans-lowbit(ans)+1 ... ans];二,当ans=2^k时, c[ans]=sum of A[1 ... ans]; 下面说明findK(k)如何运作:1,设置边界条件ans,ans'<maxn且cnt<=k:2,初始化cnt=c[ans],其中ans=2^k且k…