hdu------(4302)Holedox Eating(树状数组+二分)
Holedox Eating
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3362 Accepted Submission(s): 1145
is a small animal which can be considered as one point. It lives in a
straight pipe whose length is L. Holedox can only move along the pipe.
Cakes may appear anywhere in the pipe, from time to time. When Holedox
wants to eat cakes, it always goes to the nearest one and eats it. If
there are many pieces of cake in different directions Holedox can
choose, Holedox will choose one in the direction which is the direction
of its last movement. If there are no cakes present, Holedox just stays
where it is.
input consists of several test cases. The first line of the input
contains a single integer T (1 <= T <= 10), the number of test
cases, followed by the input data for each test case.The first line of
each case contains two integers L,n(1<=L,n<=100000), representing
the length of the pipe, and the number of events.
The next n lines,
each line describes an event. 0 x(0<=x<=L, x is a integer)
represents a piece of cake appears in the x position; 1 represent
Holedox wants to eat a cake.
In each case, Holedox always starts off at the position 0.
10 8
0 1
0 5
1
0 2
0 0
1
1
1
10 7
0 1
0 5
1
0 2
0 0
1
1
10 8
0 1
0 1
0 5
1
0 2
0 0
1
1
Case 2: 4
Case 3: 2
#include<cstring>
#include<cstdio>
#define maxn 100080
#define inf 0x3f3f3f3f
int next[maxn];
int len,n;
int lowbit(int x){
return x&(-x);
}
void update(int st,int val){
while(st<=len+){
next[st]+=val;
st+=lowbit(st);
}
}
int query(int st){
int ans=;
while(st>){
ans+=next[st];
st-=lowbit(st);
}
return ans;
} int main(){
int test;
int jud,tem,pos,ans;
//freopen("test.in","r",stdin);
//freopen("test1.in","w",stdout);s
scanf("%d",&test);
for(int i=;i<=test;i++ ){
scanf("%d%d",&len,&n);
memset(next,,sizeof(next));
bool flag=true; //开始从0开始所以必须去右边
pos=; //初始牛的位置在1初开始数
ans=;
while(n--)
{
scanf("%d",&jud);
if(!jud){
scanf("%d",&tem);
update(tem+,);
}
else{ //如果jud=1说明那条牛要吃蛋糕了
//但是不知道那边有蛋糕...
int left=pos, right=len+;
int mid;
int rr=inf; //记录最近的右边蛋糕位置
int ll=-inf; //记录最近的左边蛋糕位置
bool iseat_r=false;
int st_num=query(pos-); //开始位置以下的位置蛋糕的数目
while(left<=right){
mid=left+((right-left)>>);
if(query(mid)>st_num) {
right=mid-;
rr=mid;
iseat_r=true; //表示吃到了蛋糕
}
else
left=mid+;
}
left=;
right=pos;
st_num=query(pos);
bool iseat_l=false;
while(left<=right){
mid=left+((right-left)>>);
if(st_num-query(mid-)>){
left=mid+;
ll=mid;
iseat_l=true;
}
else right=mid-;
}
if(iseat_l||iseat_r) //又一边有蛋糕吃就可以了,否则什么都不做
{
int len1=pos-ll;
int len2=rr-pos;
if(len1<len2){
pos=ll;
ans+=len1;
flag=false;
}
else { //如果相等,真的可以随便吃吗,以继续保持原来的方向优先
if(len1>len2){
pos=rr;
ans+=len2;
flag=true;
}
else if(flag){ pos=rr;
ans+=len2;
}
else{
pos=ll;
ans+=len1;
}
}
update(pos,-); //吃掉了一个蛋糕
}
}
}
printf("Case %d: %d\n",i,ans);
}
return ;
}
hdu------(4302)Holedox Eating(树状数组+二分)的更多相关文章
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- TZOJ 4602 高桥和低桥(二分或树状数组+二分)
描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...
- POJ 2182 Lost Cows 【树状数组+二分】
题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- 树状数组+二分||线段树 HDOJ 5493 Queue
题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...
- P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]
题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...
- The Stream of Corning 2( 权值线段树/(树状数组+二分) )
题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...
- 牛客多校第3场 J 思维+树状数组+二分
牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...
- HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- HDU 2852 KiKi's K-Number 树状数组 + 二分
一共最多才100000个数,并且数值范围0~100000. 树状数组 C[i] 记录数值为 i 的数有多少个. 删除时如果Query( a ) - Query( a - 1 ) == 0 则该数不存在 ...
随机推荐
- 终端、shell、bash的区别联系
最佳答案 终端,即所谓的命令行界面,又称命令终端,用户输入shell命令用的窗口,跟Windows里的DOS界面差不多. shell,Shell就是用户和操作系统之间的壳,中介,GUI和CLI都算是S ...
- MySQL性能优化的21个最佳实践 和 mysql使用索引
1. 为查询缓存优化查询 当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一个缓存中,这样,后续的相同的查询就不用操作表而直接访问缓存结果了. 2. EXPLAIN SELECT 查询 ...
- python 内建函数功能函数 abs() coerce() divmod() round() pow()
>>> abs(-1)1>>> abs(10.) 10.0>>> abs(1.2-2.1j)2.4186773244895647>> ...
- [HDOJ3911]Black And White(线段树,区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3911 题意:一个01串,两种操作: 0 a b:查询[a,b]内连续1的最长长度. 1 a b:翻转[ ...
- DIV与IDIV的用法
DIV (unsigned divide) 无符号数除法 格式:DIV SRC 执行的操作: 字节操作:16位被除数在AX,8位除数为源操作数,结果的8位商在AL中,8位余数在AH中.表示为 (AL) ...
- EntityFramework 开始小试
1 Install-Package EntityFramework 2 创建实体类 public class Blog { public int BlogId { get; set; } public ...
- NPN&PNP
一.晶体管基础知识 晶体管分2种:NPN.PNP 晶体管通常封装为TO-92,下面是元件实物图 和 元件符合: NPN: 当电压和电流被加到基极上时,NPN晶体管: 其工作原理: 就像水龙头—给控制开 ...
- 树的计数 + prufer序列与Cayley公式 学习笔记
首先是 Martrix67 的博文:http://www.matrix67.com/blog/archives/682 然后是morejarphone同学的博文:http://blog.csdn.ne ...
- getAttribute()方法的使用小笔记
今天在某课网上复习js,突然卡在一个小例子上.反复做了几遍就是报错.终于查询w3c才找到错误的源头. 这里记录一下,谨防以后又忘了. 错误码: <body> <p id=" ...
- a++累加
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...