Holedox Eating

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3362    Accepted Submission(s): 1145

Problem Description
Holedox
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
The
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.
 
Output
Output the total distance Holedox will move. Holedox don’t need to return to the position 0.
 
Sample Input
3
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

 
Sample Output
Case 1: 9
Case 2: 4
Case 3: 2
 
Author
BUPT
 
Source
 
 
 
代码:
 #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(树状数组+二分)的更多相关文章

  1. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  2. TZOJ 4602 高桥和低桥(二分或树状数组+二分)

    描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...

  3. POJ 2182 Lost Cows 【树状数组+二分】

    题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. 树状数组+二分||线段树 HDOJ 5493 Queue

    题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...

  5. P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  6. The Stream of Corning 2( 权值线段树/(树状数组+二分) )

    题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...

  7. 牛客多校第3场 J 思维+树状数组+二分

    牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...

  8. 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 ...

  9. HDU 2852 KiKi's K-Number 树状数组 + 二分

    一共最多才100000个数,并且数值范围0~100000. 树状数组 C[i] 记录数值为 i 的数有多少个. 删除时如果Query( a ) - Query( a - 1 ) == 0 则该数不存在 ...

随机推荐

  1. JAVA排序--[插入排序]

    package com.array; public class Sort_Insert { /* * 项目名称:插入排序 ; * 项目要求:用JAVA对数组进行排序,并运用插入排序算法; * 作者:S ...

  2. Mybatis+SpringMVC+Spring整合

    1,先添加spring支持: applicationContext.xml  配在WEBINF下,四个命名空间:aop,context,tx,p 配Listener:ContextLoaderList ...

  3. [数据结构与算法]RED-BLACK(红黑)树的实现TreeMap源码阅读

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. JavaScript经典代码【二】【javascript判断用户点了鼠标左键还是右键】

    IE 下 onMouseDown 事件有个 events.button 可以返回一个数值,根据数值判断取得用户按了那个鼠标键 events.button==0 默认.没有按任何按钮. events.b ...

  5. Linux Shell脚本攻略 读书笔记

    Linux Shell脚本攻略 读书笔记 这是一本小书,总共253页,但内容却很丰富,书中的示例小巧而实用,对我这样总是在shell门前徘徊的人来说真是如获至宝:最有价值的当属文本处理,对这块我单独整 ...

  6. 容易混淆的url src href

    新手刚学习的时候会分不清 url  src  href这些,不知道什么情况下应该用哪个.现在让我来理一理. url 统一资源定位符是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网 ...

  7. CSS的基本操作

    <html> <!-- . 给整个页面填一个一个背景 . 给em添加一个样式样倾斜效果消失 . 改变第一层UL的样式为蓝色,16px . 改变第二层的UL的样式为红色 14px . ...

  8. MyEclipse中文乱码解决方法

    在Myeclipse导入一个项目,有中文乱码问题,解决方法如下: 一.将整个project设置编码UTF-8(UTF-8可以最大的支持国际化) windows->Preferences-> ...

  9. C++中关于new及内存地址的思考

    OJ题刷多了,每次都是直接分配内存,那么,你还记得怎么动态分配内存吗? ———————————————————————————————————— 我们知道,使用malloc/calloc等分配内存的函 ...

  10. zoj2589Circles(平面图的欧拉定理)

    链接 连通图中: 设一个平面图形的顶点数为n,划分区域数为r,一笔画笔数为也就是边数m,则有: n+r-m=2 那么不算外面的那个大区域的话 就可以写为 n+r-m = 1 那么这个题就可以依次求出每 ...