hdu 4302 Holedox Eating(优先队列模拟或线段树)

题目链接

Holedox Eating

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


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


题意:

 一条小虫生活在一个管子里,管子上面的某些位置会出现食物,每次小虫都会吃最近的食物,吃完之后就原地不动,等待下一次吃食物,如果有两个食物距离蛇一样远并且都是最近的,那么小虫不会掉头,而是直接按按照上一次前进的方向去吃自己前方的食物,给出给一些命令,问小虫一共走了多少路。


题解:

这一题有两个方法:

这个题目就是要迅速的找到比当前位置大的最小的那个数,和比当前位置小的最大的那个数(或者当前值本身),下面两个方法都是从这个思想入手的。

1.用两个优先队列p和p1(p中值大的优先,p1中值小的优先,这两个优先队列分别存储虫子两侧的食物信息),假设虫子当前位置为x,若此时位置p出现食物,若p<x,加入p中,否则加入p1中。那么我们可能会有个疑问,会不会在p中存在一个位置大于x呢?这个元素要么在加入时就不会在p中,要么在前面的移动过程中已经到达过了。

然后判断再删元素更新答案就行了。


2.线段树,存取每个位置上食物个数,并存区间最大值和最小值,对于一点,若没有食物,那么最大值为-1,最小值为INF,然后就是裸的线段树啦。


优先队列版本:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
struct cmp
{
	bool operator()(int a,int b)
	{
		return a>b;
	}
};
priority_queue<int,vector<int>,cmp> q1;
priority_queue<int> q;
int main()
{
	int cas;
	scanf("%d",&cas);
	for(int k=1;k<=cas;k++)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		while(!q.empty()) q.pop();
		while(!q1.empty()) q1.pop();
		int x=0,dir=1,ans=0;
		while(m--)
		{
			int op;
			scanf("%d",&op);
			if(op==0)
			{
				int p;
				scanf("%d",&p);
				if(p<=x) q.push(p);
				else q1.push(p);
			}
			else
			{
				if(!q.empty()&&!q1.empty())
				{
					int t1=q.top(),t2=q1.top();
					if(x-t1<t2-x)
					{
						q.pop();
						ans+=x-t1;
						x=t1;
						dir=-1;
					}
					else if(x-t1>t2-x)
					{
						q1.pop();
						ans+=t2-x;
						x=t2;
						dir=1;
					}
					else
					{
						if(dir==1)
						{
							q1.pop();
							ans+=t2-x;
							x=t2;
						}
						else
						{
							q.pop();
							ans+=x-t1;
							x=t1;
						}
					}
				}
				else if(!q.empty())
				{
					int t=q.top();
					q.pop();
					ans+=x-t;
					x=t;
					dir=-1;
				}
				else if(!q1.empty())
				{
					int t=q1.top();
					q1.pop();
					ans+=t-x;
					x=t;
					dir=1;
				}
			}
		}
		printf("Case %d: %d\n",k,ans);
	}
	return 0;
}

线段树版本:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN=100000+100;
const int INF=0x3fffffff;
struct node
{
	int l,r,t,mx,mi;
}seg[MAXN*4];
void build(int i,int l,int r)
{
	seg[i].l=l,seg[i].r=r,seg[i].t=0;
	seg[i].mx=-1,seg[i].mi=INF;
	if(l==r)
	return;
	int m=(l+r)>>1;
	build(i*2,l,m),build(i*2+1,m+1,r);
}
void push_up(int i)
{
	if(seg[i].l==seg[i].r) return;
	seg[i].mx=max(seg[i*2].mx,seg[i*2+1].mx);
	seg[i].mi=min(seg[i*2].mi,seg[i*2+1].mi);
}
void add(int i,int p)
{
	if(seg[i].l==seg[i].r)
	{
		seg[i].t++;
		seg[i].mx=seg[i].mi=p;
		return;
	}
	int m=(seg[i].l+seg[i].r)>>1;
	if(p<=m) add(i*2,p);
	else add(i*2+1,p);
	push_up(i);
}
void del(int i,int p)
{
	if(seg[i].l==seg[i].r)
	{
		seg[i].t--;
		if(!seg[i].t)
		{
			seg[i].mx=-1;
			seg[i].mi=INF;
		}
		return;
	}
	int m=(seg[i].l+seg[i].r)>>1;
	if(p<=m) del(i*2,p);
	else del(i*2+1,p);
	push_up(i);
}
int query1(int i,int l,int r)
{
	if(seg[i].l==l&&seg[i].r==r)
	return seg[i].mx;
	int m=(seg[i].l+seg[i].r)>>1;
	if(r<=m) return query1(i*2,l,r);
	else if(l>m) return query1(i*2+1,l,r);
	else return max(query1(i*2,l,m),query1(i*2+1,m+1,r));
}
int query2(int i,int l,int r)
{
	if(seg[i].l==l&&seg[i].r==r)
	return seg[i].mi;
	int m=(seg[i].l+seg[i].r)>>1;
	if(r<=m) return query2(i*2,l,r);
	else if(l>m) return query2(i*2+1,l,r);
	else return min(query2(i*2,l,m),query2(i*2+1,m+1,r));
}

int main()
{
	int cas;
	scanf("%d",&cas);
	for(int k=1;k<=cas;k++)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		build(1,0,n);
		int x=0,dir=1,ans=0;
		while(m--)
		{
			int op;
			scanf("%d",&op);
			if(op==0)
			{
				int p;
				scanf("%d",&p);
				add(1,p);
			}
			else
			{
				int t1=query1(1,0,x),t2=query2(1,x,n);
				if(t1!=-1&&t2!=INF)
				{
					if(x-t1<t2-x)
					{
						dir=-1;
						del(1,t1);
						ans+=x-t1;
						x=t1;
					}
					else if(x-t1>t2-x)
					{
						dir=1;
						del(1,t2);
						ans+=t2-x;
						x=t2;
					}
					else
					{
						if(dir==1)
						{
							del(1,t2);
							ans+=t2-x;
							x=t2;
						}
						else
						{
							del(1,t1);
							ans+=x-t1;
							x=t1;
						}
					}
				}
				else if(t1!=-1&&t2==INF)
				{
					del(1,t1);
					ans+=x-t1;
					x=t1;
					dir=-1;
				}
				else if(t2!=INF&&t1==-1)
				{
					del(1,t2);
					ans+=t2-x;
					x=t2;
					dir=1;
				}
			}
		}
		printf("Case %d: %d\n",k,ans);
    }
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值