HDU 4302 Holedox Eating (STL + 模拟)
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.
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=0; //loc代表自己的位置,dir==0表示向左,dir==1表示向右,ans为答案 void Forward(){ //向前走 int tmp=q1.top(); q1.pop(); ans+=tmp-loc; loc=tmp; dir=1; } void Back(){ //向后走 int tmp=q2.top(); q2.pop(); ans+=loc-tmp; loc=tmp; dir=0; } int main(){ //freopen("input.txt","r",stdin); int t,cases=0; scanf("%d",&t); while(t--){ while(!q1.empty()) q1.pop(); while(!q2.empty()) q2.pop(); scanf("%d%d",&L,&n); loc=0,dir=1,ans=0; int op,x; while(n--){ scanf("%d",&op); if(op==0){ 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==1) Forward(); else Back(); } } } } printf("Case %d: %d\n",++cases,ans); } return 0; }
分类:
STL
标签:
acm/icpc hdu
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
· 程序员常用高效实用工具推荐,办公效率提升利器!