HDOJ Holedox Eating 4302(优先队列)

来源:互联网 发布:免费洗车软件2016 编辑:程序博客网 时间:2024/05/29 08:10

Holedox Eating

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


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
310 80 10 510 20 0111 10 70 10 510 20 01110 80 10 10 510 20 011
 

Sample Output
Case 1: 9Case 2: 4Case 3: 2
 

Author
BUPT
 

Source
2012 Multi-University Training Contest 1
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4300 4301 4303 4304 4308 

题意:选最近的蛋糕,如果距离一样,方向安上次的方向。

#include<stdio.h>#include<algorithm>#include<queue>#include<vector>#include<string.h>using namespace std;int main(){int T;scanf("%d",&T);int num=1;while(T--){priority_queue<int,vector<int>,greater<int> >q1;priority_queue<int>q2;int L,n;int s=0;int ans=0;int t=1;scanf("%d%d",&L,&n);while(n--){int temp,x;scanf("%d",&temp);if(temp==0){scanf("%d",&x);if(x>=s) q1.push(x);else q2.push(x);}else if(temp==1&&!q1.empty()&&!q2.empty()){int p1=q1.top();int p2=q2.top();if(p1-s>s-p2){t=-1;ans+=s-p2;s=p2;q2.pop();}else if(p1-s<s-p2){t=1;ans+=p1-s;s=p1;q1.pop();}else if(t==1){ans+=p1-s;s=p1;q1.pop();}else{ans+=s-p2;s=p2;q2.pop();}}else if(temp==1&&!q1.empty()){t=1;ans+=q1.top()-s;s=q1.top();q1.pop();}else if(temp==1&&!q2.empty()){t=-1;ans+=s-q2.top();s=q2.top();q2.pop();}}printf("Case %d: %d\n",num++,ans);}return 0; } 

提交记得选G++。。。。c++会ce。
 
0 0
原创粉丝点击