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 则该数不存在 ...
随机推荐
- DirectX 3d 取景变换
在世界坐标系中,几何体和摄像机都是相对于世界坐标系定义的.但是当摄像机的位置和朝向任意时,投影变换及其它类型的变的就略显困难或效率不高.为了简化运算,我们将摄像机变的至世界坐标系原点,并将其旋转,使摄 ...
- Get_File_Name Usage in Oracle Forms 6i
Get_File_Name is built-in function of Oracle Forms 6i, used to get the file name with address by bro ...
- 通过数据库和EasyUI的combobox级联实现省市区三级联动
1.新建一个web项目 2.因为这里用到了数据库所以我们在lib目录导入Hibernate的jar包.fastjson.jar包及数据库jar包 3.同样导入EasyUI的组件配置,并在新建的html ...
- poj 2954 Triangle(Pick定理)
链接:http://poj.org/problem?id=2954 Triangle Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- FZU 2221 RunningMan(跑男)
Problem Description 题目描述 ZB loves watching RunningMan! There's a game in RunningMan called 100 vs 10 ...
- 使用Invoke、委托函数
//Invoke(new MethodInvoker(delegate() //{ // DataBind(); //}));
- MongoDB开发学习
如果你从来没有接触MongoDB或对MongoDB有一点了解,如果你是C#开发人员,那么你不妨花几分钟看看本文.本文将一步一步带您轻松入门. 阅读目录 一:简介 二:特点 三:下载安装和开启服务器 四 ...
- xml语法、DTD约束xml、Schema约束xml、DOM解析xml
今日大纲 1.什么是xml.xml的作用 2.xml的语法 3.DTD约束xml 4.Schema约束xml 5.DOM解析xml 1.什么是xml.xml的作用 1.1.xml介绍 在前面学习的ht ...
- 利用EL表达式+JSTL在客户端取得数据 示例
<%@page import="cn.gbx.domain.Address"%> <%@page import="cn.gbx.domain.User& ...
- Joke
你忍心蜗居在不到10平米的小屋里吗?你忍心看着自己的女友和你奋斗一辈子还供不起一套房吗?你忍心看着你父母缩衣节食把仅有的一点养老金帮你还房贷吗? 这里才是实现你梦想的地方.加入我们,待遇从优,装备齐全 ...