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 则该数不存在 ...
随机推荐
- hihoCoder太阁最新面经算法竞赛17
比赛链接:http://hihocoder.com/contest/hihointerview26 A.排序后枚举两个点,确定一个矩形后二分剩下两个点. #include <bits/stdc+ ...
- Shell概述
一,Shell原理
- [SAP ABAP开发技术总结]局部变量、全局变量
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- poj 3335(半平面交)
链接:http://poj.org/problem?id=3335 //大牛们常说的测模板题 ------------------------------------------------- ...
- MQ使用几个命令
一.MQ常见基本指令: MQ现在使用的比较常见,但是在测试过程中,使用命令行直接查询,有时候事半功倍. activemq-admin stop activemq-admin list activemq ...
- c trans
#define BUFSIZE 100 char buf[BUFSIZE]; ; int getch(void) { )? buf[--bufp] : getchar(); } void ungetc ...
- poj2451Uyuw's Concert(半平面交)
链接 逆时针给出线段,如果模板是顺时针的修改下系数的符号进行平面交即可. #include <iostream> #include<cstdio> #include<cs ...
- CentOS下源码安装MySQL
一.创建mysql用户与组,相关目录 useradd mysql -s /sbin/nologin mkdir /usr/local/mysql chown -R mysql.mysql mkdir ...
- (二)miller指导查看主控板寄存器操作
Welcome to Command Shell!Username:admin Password:***** ROS>en ROS# ROS# ROS# ROS# ROS#^ada ROS(ad ...
- hdu 5340 Three Palindromes
前几晚 BC 的第二题,官方给出的题解是: 然后我结合昨天刚看的 Manacher 算法试着写了下,发现 pre.suf 数组挺难构造的,调试了好久,然后就对中间进行枚举了,复杂度应该是 O(n2) ...