两个优先队列维护左右(12北邮多校)(4302)

Holedox Eating

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


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

首先明白移动不可能跨过某个蛋糕。这里用了很巧妙的方法:用两个优先队列维护当前点的左右的蛋糕,同时题目还要求记录方向,直接用face标记,向左是1,向右是0。记得要清空,因为两个队列申请在主函数外部。


/*------------------Header Files------------------*/
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <limits.h>
using namespace std;
/*------------------Definitions-------------------*/
#define LL long long
#define PI acos(-1.0)
#define INF 0x3F3F3F3F
#define MOD 10E9+7
#define MAX 500050
/*---------------------Work-----------------------*/
priority_queue<int>ql;
priority_queue<int,vector<int>,greater<int> >qr;
void work()
{
	int T; cin>>T;
	for(int Case=1;Case<=T;Case++)
	{
		int L,n;
		scanf("%d%d",&L,&n);
		//清空队列 
		while(!ql.empty()) ql.pop();
		while(!qr.empty()) qr.pop();
		int now=0,sum=0,face=0,ans,location;
		while(n--)
		{
			scanf("%d",&ans);
			if(ans==0)
			{
				scanf("%d",&location);
				if(location>=now) qr.push(location);
				else ql.push(location);
			}
			else
			{
				if(ql.empty()&&qr.empty()) continue;
				if(qr.empty())
				{
					sum+=(now-ql.top());
					now=ql.top();
					face=1;
					ql.pop();
				}
				else if(ql.empty())
				{
					sum+=(qr.top()-now);
					now=qr.top();
					face=0;
					qr.pop();
				}
				else if(now-ql.top()==qr.top()-now)
				{
					if(face)
					{
						sum+=(now-ql.top());
						now=ql.top();
						ql.pop();
					}
					else
					{
						sum+=(qr.top()-now);
						now=qr.top();
						qr.pop();
					}
				}
				else if(now-ql.top()<qr.top()-now)
				{
					sum+=(now-ql.top());
					face=1;
					now=ql.top();
					ql.pop();
				}
				else
				{
					sum+=(qr.top()-now);
					face=0;
					now=qr.top();
					qr.pop();
				}
			}
		}
		printf("Case %d: %d\n",Case,sum);
	}
}
/*------------------Main Function------------------*/
int main()
{
	//freopen("test.txt","r",stdin);
	//freopen("cowtour.out","w",stdout);
	//freopen("cowtour.in","r",stdin);
	work();
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值