【ZCMU1052】Holedox Eating(优先队列)

点击打开链接

 

1052: Holedox Eating

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 60  Solved: 53
[Submit][Status][Web Board]

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

HINT

Source

2012 Multi-University Training Contest 1

 

解题思路:

用到优先队列,将每次大于所在点的位置进入小的先出的优先队列,小于所在点位置的进入大的先出的优先队列。计算两边距离,若相等则按照上一次的方向。当某个队列为空时,只使用不为空的队列。

 

代码:

#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
int main()
{
	int repeat,num=0;
	cin>>repeat;
	while(repeat--){
		num++;
		priority_queue< int,vector<int>,greater<int> >q1;
		priority_queue<int>q2;
		int l,n,i,t,x,s=0,d1,d2,flag=1;
		int ans=0;
		cin>>l>>n;
		for(i=0;i<n;i++){
			cin>>t;
			if(t==0){
				cin>>x;
				if(x>=s)q1.push(x);
				else q2.push(x);
			}
			else if(t==1 && !q1.empty() && !q2.empty()){
				//cout<<"..."<<endl;
				d1=q1.top();d2=q2.top();
				if(d1-s>s-d2){
					flag=-1;
					ans=ans+s-d2;
					s=d2;
					q2.pop();
				}
				else if(d1-s<s-d2){
					flag=1;
					ans=ans+d1-s;
					s=d1;
					q1.pop();
				}
				else if(d1-s==s-d2 && flag==1){
					ans=ans+d1-s;
					s=d1;
					q1.pop();
				}
				else if(d1-s==s-d2 && flag==-1){
					ans=ans+s-d2;
					s=d2;
					q2.pop();
				}
			}
			else if(t==1 && !q1.empty()){
				flag=1;
				ans=ans+q1.top()-s;
				s=q1.top();
				q1.pop();
			}
			else if(t==1 && !q2.empty()){
				flag=-1;
				ans=ans+s-q2.top();
				s=q2.top();
				q2.pop();
			}
			//cout<<"s="<<s<<"ans="<<ans<<endl;
		}
		cout<<"Case "<<num<<": "<<ans<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值