现在的位置: 首页 > 综合 > 正文

hdu 4302 Holedox Eating

2018年01月15日 ⁄ 综合 ⁄ 共 2687字 ⁄ 字号 评论关闭

Holedox Eating

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

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
 

Source
 

Recommend
zhuyuanchen520

知识点:线段数

算是比较好的一个线段树了,比赛的时候没有写出来,主要是对于线段数的灵活运用还不够,虽然想到了用线段树,也用线段树实现了,但是一直WA,对于线段树的理解还不深吧。。。

对于树中结点所代表的信息没有灵活运用,比赛的时候是把结点所拥有的蛋糕数作为结点信息,赛后别人的AC代码是距离宠物最近的蛋糕位置作为结点信息.明显别人的要好些,比赛的时候也有想到这么做,只是当时以为不能这样实现。。。。。

#include<stdio.h>
#include<string.h>

#define INF 10000000
#define MAXN 100001
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

int nn[MAXN<<2];
int num[MAXN];
int n,op;

int abs(int x)
{
	if(x>0)
		return x;
	return -x;
}

void PushUp(int rt)
{
	if(abs(nn[rt<<1]-n)<abs(nn[rt<<1|1]-n))
		nn[rt]=nn[rt<<1];
	if(abs(nn[rt<<1]-n)>abs(nn[rt<<1|1]-n))
		nn[rt]=nn[rt<<1|1];
	if(abs(nn[rt<<1]-n)==abs(nn[rt<<1|1]-n))
		if(op==1)
			nn[rt]=nn[rt<<1|1];
		else
			nn[rt]=nn[rt<<1];
//	printf("%d %d\n",rt,nn[rt]);
}

void build(int l,int r,int rt)
{
	nn[rt]=INF;
	if(l==r)
		return ;
	int m=(l+r)>>1;
	build(lson);
	build(rson);
}

void update(int x,int tmp,int l,int r,int rt)
{
	if(l==r)
	{
		nn[rt]=tmp;
//		printf("%d %d\n",rt,tmp);
		return ;
	}
	int m=(l+r)>>1;
	if(x<=m)
		update(x,tmp,lson);
	else
		update(x,tmp,rson);
	PushUp(rt);
}

int main(void)
{
	int T,t,s;
//	freopen("d:\\in.txt","r",stdin);
	scanf("%d",&T);
	for(t=1;t<=T;t++)
	{
		int L,m;
		scanf("%d%d",&L,&m);
		memset(num,0,sizeof(num));
		build(0,L,1);
		s=0,n=0;
		op=1;				//代表方向
		int a,b;
		while(m--)
		{
			scanf("%d",&a);
			if(a==0)
			{
				scanf("%d",&b);
				num[b]++;
				update(b,b,0,L,1);
			}
			else
			{
//				printf("%d\n",nn[1]);	
				if(nn[1]!=INF)
				{
//					printf("OK\n");
					if(nn[1]>n)
						op=1;
					if(nn[1]<n)
						op=-1;
					s+=abs(nn[1]-n);
//					printf("%d %d\n",nn[1],s);
					n=nn[1];
					if(num[nn[1]]>0)
						num[n]--;
					if(num[n]==0)
						update(n,INF,0,L,1);
				}
			}
		}
		printf("Case %d: %d\n",t,s);

	}
	return 0;
}

抱歉!评论已关闭.