19市赛 树状数组 第k个小的糖果
int find_kth(int k)
{
int ans = , cnt = , i;
for (i = ; i >= ; i--)/
{
ans += ( << i);
if (ans >= maxn|| cnt + c[ans] >= k)
ans -= ( << i);
else
cnt += c[ans];
}
return ans + ;
}
题意:
口袋里可以放进不同大小的糖果,也可以拿出不同大小的糖果。查询过程中第k小的糖果体积是多少
输入T,有T组,接下来是输入m次操作
m行输入op和x
当op等于1的时候,放进x大小的糖果
当op等于2的时候,拿进x大小的糖果
当op等于3的时候,查询第x小的糖果,并输出答案
思路:二分+树状数组(比赛时限)
这里写一个优化的树状数组的写法
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define ll long long
#define lowbit(b) b&(-b)
using namespace std;
const int maxn=1e5+;
int c[maxn],f[];
inline int read() {
char ch = getchar(); int x = , f = ;
while(ch < '' || ch > '') {
if(ch == '-') f = -;
ch = getchar();
} while('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
} return x * f;
}
inline void updata(int x, int y){
for (int i = x; i <= maxn; i += lowbit(i)){
c[i] += y;
}
}
int main()
{
int t,i;f[]=;
for(i=;i<=;i++){
f[i]=f[i-]*;
}
t=read();
while(t--){
for(i=;i<maxn;i++){c[i]=;}
int m;
m=read();
for(i=;i<m;i++){
int op,x;
op=read();x=read();
if(op==){
updata(x,);
}
else if(op==){
updata(x,-);
}
else{
int ans=,sum=,j;
for(j=;j>=;j--){
ans+=f[j];
if(ans>=maxn || sum+c[ans]>=x){
ans-=f[j];
}
else{
sum+=c[ans];
}
}
printf("%d\n",ans+);
}
}
}
return ;
}
/*
2
3
1 2
1 2
3 1
4
1 2
1 3
2 2
3 1
*/
19市赛 树状数组 第k个小的糖果的更多相关文章
- POJ2828 Buy Tickets[树状数组第k小值 倒序]
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19012 Accepted: 9442 Desc ...
- hdu 5493 Queue 树状数组第K大或者二分
Queue Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- XJTUOJ wmq的队伍(树状数组求 K 元逆序对)
题目链接:http://oj.xjtuacm.com/problem/14/[分析]二元的逆序对应该都会求,可以用树状数组.这个题要求K元,我们可以看成二元的.我们先从后往前求二元逆序对数, 然后对于 ...
- hrbust 1840 (树状数组第k大) 删点使用
小橙子 Time Limit: 2000 MS Memory Limit: 32768 K Total Submit: 2(2 users) Total Accepted: 1(1 users) Ra ...
- Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组
Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, ea ...
- BZOJ.3110.[ZJOI2013]K大数查询(整体二分 树状数组/线段树)
题目链接 BZOJ 洛谷 整体二分求的是第K小(利用树状数组).求第K大可以转为求第\(n-K+1\)小,但是这样好像得求一个\(n\). 注意到所有数的绝对值\(\leq N\),将所有数的大小关系 ...
- 树状数组求第K小值 (spoj227 Ordering the Soldiers && hdu2852 KiKi's K-Number)
题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? ...
- 4.9 省选模拟赛 划分序列 二分 结论 树状数组优化dp
显然发现可以二分. 对于n<=100暴力dp f[i][j]表示前i个数分成j段对于当前的答案是否可行. 可以发现这个dp是可以被优化的 sum[i]-sum[j]<=mid sum[i] ...
- bzoj4361 isn(树状数组优化dp+容斥)
4361: isn Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 938 Solved: 485[Submit][Status][Discuss] ...
随机推荐
- 用js实现鼠标点击爱心特效
效果如图以下是代码 <script> !function(e, t, a) { function r() { for (var e = 0; e < s.length; e++) s ...
- python接口自动化测试 - requests库的基础使用
简单介绍 requests库简单易用的HTTP库 Get请求 格式: requests.get(url) 注意:若需要传请求参数,可直接在 url 最后的 ? 后面,也可以调用 get() 时多加一个 ...
- render方法渲染组件和在webpack中导入vue
使用component注册的组件div容器里可以放多个,但是使用render的只能放一个 <div id="app"> <p>我可以放两个</p> ...
- 如何通过源码包的方式在linux安装python36
背景: python34的安装非常简单,直接用yum就可以安装,但是安装最新版的python36通过yum方式是不行的,需要通过源码包进行安装 具体步骤如下: 1.安装openssl静态库[pip3安 ...
- opencv:图像轮廓发现
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- python eval() 进行条件匹配
最近开发一个功能,根据条件表达式过滤数据,其中用到了eval(条件字符串,字典) 发现一个现象: >>> print u"campGrade in [ '\u51cf\u8 ...
- 如何使用 idea提交svn代码
链接:https://jingyan.baidu.com/article/b2c186c80d81b1c46ff6ff59.html
- Git - 03. git 工作空间
1. 概述 git 存放代码的地方 2. 创建 命令 # 1. 从无到有 > git init # 2. 从远程拉去现有的仓库 > git clone <url> 3. 文件生 ...
- ENS中文文档系列之三 [ ENS常见问题 ]
原文地址:https://ensuser.com/docs/frequently-asked-questions.html更多最新信息,请前往 ENS 中文服务站点:ENSUser 关于 ENS 注册 ...
- jvm01
hotspot:是jvm的核心组件(或者名称),jvm 需要对class文件进行编译成cpu能直接运行的代码.hotspot会对频繁使用的class代码进行缓存,不会再次编译,类似于缓存 client ...