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 则该数不存在 ...
随机推荐
- 修改数据库表的schema,(表的[dbo.]前缀)
数据库使用过程中遇到这种问题,请看下图
- 样式表中的 element.style样式如何修改
我们在写前面 web样式的时候,会发现有些时候,我们怎么修改 style里面的值,页面上的样式都不会修改,当你用工具查看时,会发现里面会有 element.style的值,这个值还找不到是在哪里出现的 ...
- Servlet与Tomcat
Web应用不仅局限于展示在服务器上的静态页面,更多的是根据用的请求动态的生成页面信息,还可以从数据库中提取数据,生成页面返回给用户. 第一种方法:遵循HTTP协议实现一个服务器端软件 第二种方法:利用 ...
- [数据结构与算法]哈夫曼(Huffman)树与哈夫曼编码
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- .net 常用Response.ContentType
来源:http://blog.csdn.net/navy235/article/details/5982319 不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 ...
- php 上传文件。$_FILES
<form name="article" method="post" enctype="multipart/form-data" ac ...
- ubuntu使用mailx利用SMTP发送邮件
转载:http://www.blogjava.net/jasmine214--love/archive/2010/10/09/334102.htmlLinux下mail利用外部邮箱发送邮件的方法: 1 ...
- Android ActionBar以及menu的代码设置样式
menu部分xml代码 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android= ...
- jquery.pagination.js分页
参数说明 参数名 描述 参数值 maxentries 总条目数 必选参数,整数 items_per_page 每页显示的条目数 ...
- 自定义获取焦点的TextView
自定义控件编写流程 创建一个默认就能获取焦点的TextView 1.创建一个类继承至TextView,FocusTextView 2.重写其构造方法 //使用在通过java代码创建控件 public ...