Hduoj4302【优先队列】
来源:互联网 发布:ipad cellular 知乎 编辑:程序博客网 时间:2024/06/13 06:39
/*Holedox Eating Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)Total Submission(s) : 7 Accepted Submission(s) : 4Problem DescriptionHoledox 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. InputThe 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. OutputOutput the total distance Holedox will move. Holedox don’t need to return to the position 0. Sample Input310 80 10 510 20 0111 10 70 10 510 20 01110 80 10 10 510 20 011 Sample OutputCase 1: 9Case 2: 4Case 3: 2AuthorBUPT Source2012 Multi-University Training Contest 1*//*HDU 4302G++ 281ms 532K*/#include<stdio.h>#include<queue>#include<iostream>#include<algorithm>using namespace std;struct cmp{ bool operator()(int x,int y) { return x>y;//从小到大 }};priority_queue<int,vector<int>,cmp>q;//从大到小队列 priority_queue<int>q2;//从小到大队列 int main(){ int T, L, n; int A, B; int iCase = 0; scanf("%d",&T); while(T--) { iCase++; scanf("%d%d", &L, &n); while(!q.empty()) q.pop();//清空 while(!q2.empty()) q2.pop();//清空 int x=0, ans=0, t=1; while(n--) { scanf("%d", &A); if(A == 0) { scanf("%d", &B); if(B >= x ) q.push(B);//如果cake在当前位置的右边放入优先队列 else q2.push(B);//否则直接放入队列 } else { if( !q.empty() && !q2.empty() ) { int temp1 = q.top(); int temp2 = q2.top(); if(temp1-x < x-temp2)//如果右边的蛋糕近 { t = 1;//方向向右 ans += q.top() - x;//记录距离 x = q.top();//更改当前位置 q.pop();//删除 } else if(temp1-x > x-temp2)//如果左边的蛋糕近 { t = -1;//方向向左 ans += x - q2.top(); x = q2.top();//更改当前位置 q2.pop();//删除 } else if(t == 1)//如果一样近并且上次方向向右 { ans += q.top() - x;//右移 x = q.top();//更改当前位置 q.pop();//删除 } else { ans += x - q2.top();//左移 x = q2.top();//更改当前位置 q2.pop();//删除 } } else if( !q.empty() )//如果左边没有蛋糕 { t = 1;//方向向右 ans += q.top() - x;//右移 x = q.top();//更改当前位置 q.pop();//删除 } else if( !q2.empty() )//如果右边没有 蛋糕 { t = -1;//方向向左 ans += x-q2.top();//左移 x = q2.top();//更改当前位置 q2.pop();//删除 } } } printf("Case %d: %d\n", iCase, ans); } return 0;}
题意:给出一根管子,从0开始编号到L,给出2种命令,一种是在某个编号处放蛋糕,一种是老鼠吃蛋糕,老鼠的初始位置为0,并且老鼠会选择最近的蛋糕吃,当左右的蛋糕一样近时,则继续以原来的方向前进,吃完所有蛋糕后不需要回到0处,问移动距离。
思路:用2个有限队列分别表示老鼠当前位置左右的蛋糕位置,并且始终可以挑选出距离当前位置最近的蛋糕,记录吃完所有蛋糕所花的时间。
0 0
- Hduoj4302【优先队列】
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- C++设计模式之迭代器模式
- 初识正则表达式(数量,金额)
- 存储过程调试_1
- Git资料记录
- c++调用Java以及string互转
- Hduoj4302【优先队列】
- AOSP---JS版本JDK开发
- 用R语言进行关联分析
- android制作一个简单的天气预报
- 在家创业赚钱告诉你一个月8000元能干啥?
- python对象特殊方法列表
- 第二周项目-2
- 图像特征
- Android ListView CheckBox状态错乱