Holedox Eating

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

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
 
Recommend
zhuyuanchen520
 
题意:在一条直线上一开始一只小动物在0点,然后有两种操作,一种是在某一点上放一块蛋糕,另一种是去吃一块蛋糕。其中吃蛋糕有规定,先吃离小动物最近的蛋糕,如果有两块蛋糕离小动物的距离一样近,先吃当时的朝向的蛋糕,问所有操作以后小动物所走的路多长。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; int L,n;
priority_queue<int,vector<int>,greater<int> >q1; //升序,(比loc大的,即蛋糕在自己位置的右边)
priority_queue<int> q2; //降序,(比loc小的,即蛋糕在自己位置的左边)
int loc,dir,ans=; //loc代表自己的位置,dir==0表示向左,dir==1表示向右,ans为答案 void Forward(){ //向前走
int tmp=q1.top();
q1.pop();
ans+=tmp-loc;
loc=tmp;
dir=;
} void Back(){ //向后走
int tmp=q2.top();
q2.pop();
ans+=loc-tmp;
loc=tmp;
dir=;
} int main(){ //freopen("input.txt","r",stdin); int t,cases=;
scanf("%d",&t);
while(t--){
while(!q1.empty())
q1.pop();
while(!q2.empty())
q2.pop();
scanf("%d%d",&L,&n);
loc=,dir=,ans=;
int op,x;
while(n--){
scanf("%d",&op);
if(op==){
scanf("%d",&x);
if(x>=loc)
q1.push(x);
else
q2.push(x);
}else{
if(q1.empty() && q2.empty()) //没有蛋糕则什么也不做
continue;
if(q1.empty() && !q2.empty()){
Back();
continue;
}
if(!q1.empty() && q2.empty()){
Forward();
continue;
}
if(!q1.empty() && !q2.empty()){
int tmp1=q1.top();
int tmp2=q2.top();
if(tmp1-loc>loc-tmp2)
Back();
else if(tmp1-loc<loc-tmp2)
Forward();
else{
if(dir==)
Forward();
else
Back();
}
}
}
}
printf("Case %d: %d\n",++cases,ans);
}
return ;
}

HDU 4302 Holedox Eating (STL + 模拟)的更多相关文章

  1. HDU 4302 Holedox Eating (线段树模拟)

    题意:一个老鼠在一条长度为L的直线上跑,吃蛋糕,老鼠只能沿直线移动.开始时没有蛋糕,老鼠的初始位置是0. 有两个操作,0 x 代表在位置x添加一个蛋糕: 1 代表老鼠想吃蛋糕.老鼠每次都会选择离自己最 ...

  2. hdu 4302 Holedox Eating

    http://acm.hdu.edu.cn/showproblem.php?pid=4302 #include <cstdio> #include <cstring> #inc ...

  3. HDU 4302 Holedox Eating(multiset)

    http://acm.hdu.edu.cn/showproblem.php?pid=4302 题意: 在一条直线上,会有多条命令,如果是0,那么就会在x位置处出现一个蛋糕,如果是1,某人就会找到最近的 ...

  4. hdu 4302 Holedox Eating(优先队列/线段树)

    题意:一只蚂蚁位与原点,在x轴正半轴上会不时地出现一些蛋糕,蚂蚁每次想吃蛋糕时选取最近的去吃,如果前后距离相同,则吃眼前的那一块(即方向为蚂蚁的正前),求最后蚂蚁行进距离. 思路:优先队列q存储蚂蚁前 ...

  5. Holedox Eating HDU4302 模拟

    Problem Description Holedox is a small animal which can be considered as one point. It lives in a st ...

  6. multiset || 线段树 HDOJ 4302 Holedox Eating

    题目传送门 题意:一个长度L的管子,起点在0.n次操作,0 p表示在p的位置放上蛋糕,1表示去吃掉最近的蛋糕(如果左右都有蛋糕且距离相同,那么吃同方向的蛋糕),问最终走了多少路程 分析:用multis ...

  7. hdu------(4302)Holedox Eating(树状数组+二分)

    Holedox Eating Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. stl+模拟 CCF2016 4 路径解析

    // stl+模拟 CCF2016 4 路径解析 // 一开始题意理解错了.... #include <iostream> #include <string> #include ...

  9. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

随机推荐

  1. 转 :scikit-learn的GBDT工具进行特征选取。

    http://blog.csdn.net/w5310335/article/details/48972587 使用GBDT选取特征 2015-03-31 本文介绍如何使用scikit-learn的GB ...

  2. js 正则表验证输入

    输入1-9的整数 <input type="text" class="form-control" style="width: 60px;disp ...

  3. RabbitMQ的应用场景以及基本原理介绍 【转】

    http://blog.csdn.net/whoamiyang/article/details/54954780 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 背景 应用 ...

  4. JS调试必备的5个debug技巧_javascript技巧

    JS调试必备的debug调试javascript技巧 1. debugger; 我以前也说过,你可以在JavaScript代码中加入一句debugger;来手工造成一个断点效果.需要带有条件的断点吗? ...

  5. C#.NET常见问题(FAQ)-如何设置控件水平对齐,垂直对齐

    如果要设置一些控件垂直对齐,点击这个按钮   如果要设置水平对齐,则点击这个按钮,选中控件之后点击左对齐(多个按钮都试下吧,总归能对齐到你要的效果的)   更多教学视频和资料下载,欢迎关注以下信息: ...

  6. Linux网络编程:基于UDP的程序开发回顾篇

    基于无连接的UDP程序设计 同样,在开发基于UDP的应用程序时,其主要流程如下:   对于面向无连接的UDP应用程序在开发过程中服务端和客户端的操作流程基本差不多.对比面向连接的TCP程序,服务端少了 ...

  7. python获取命令行参数 启动文件

    import sys # python D:\MARK\temp.py m1 start 9090 print(sys.argv) # ['D:\\MARK\\temp.py', 'm1', 'sta ...

  8. Win10 PLSQL 登录后,提示数据库字符集(AL32UTF8)和客户端字符集(ZHS16GBK)不一致

    plsql 登录后提示: Database character set (AL32UTF8) and Client character set (ZHS16GBK) are different.Cha ...

  9. Android——碎片事务调用失败

    遇到一个情况: 在一个定时器中,每秒发起一次网络请求查询订单支付状态,如果支付成功,则在response中发出一条信息: handler中收到信息,则控制碎片切换. 结果遇到了一个错误:就是碎片切换事 ...

  10. 在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean

    1.有时候在项目中需要定时启动某个任务,对于这个需求,基于JavaEE规范,我们可以使用Listener与TimerTask来实现,代码如下: public class TestTaskListene ...