1109 - Mummy Madness

During an excursion to the desert at the 2011 ACM-ICPC World Finals, you come across an old Egyptian tomb. Unfortunately, opening the tomb turns out to be a bad idea: all of a sudden, what was just a few moments ago an empty desert has now become a desert crawling with grumpy mummies (you would be grumpy too if you were suddenly awakened after a few thousand years of peaceful sleep).


Faced with this murderous mass of mad mummies, your only chance is to run for it and try to escape before they catch you. The question is: how long will it take before a mummy catches you, assuming neither you nor the mummies ever get tired?

We model the desert as a grid of squares. You and the mummies take turns making moves on the grid. You make the first move. In your turns, you can move to any of the eight squares adjacent to your current location, or you can choose to stand still. In the mummies' turns, each mummy simply moves to the adjacent square that brings it closest to you (measured by Euclidean distance, assuming that you and all the mummies stand in the centers of their respective squares). It is possible for two mummies to occupy the same square.

A time step consists of your move followed by the mummies' moves. A mummy catches you if it moves to the square where you are located, or if you move to the square occupied by the mummy. Of course, you try to avoid being caught for as long as possible. After how many time steps will you be caught?

\epsfbox{p5136.eps}

Figure I.1: A mummy chase

The figure illustrates what might happen if you are being chased by four mummies. The square labeled H is your initial position, and the squares labeled M are the initial positions of mummies. After four time steps, you are caught by the mummy whose initial position was (3, 4) with respect to your initial position.

Input 

The input consists of several test cases. Each test case begins with an integer n (0$ \le$n$ \le$105) giving the number of mummies in the desert. The following n lines each contain two integers x and y, indicating that there is initially a mummy at coordinates (xy) of the desert, where x and y are both bounded by 106 in absolute value. Your starting position is (0, 0), and no mummy starts at this position.

The last test case is followed by a line containing the number `-1'.

Output 

For each test case, display its test case number followed by the maximum number of time steps until you are caught (measured as the total number of turns that you get), or the word ``never" if you can avoid capture indefinitely.

Follow the format of the sample output.

Sample Input 

4
-3 5
3 4
-6 -2
1 -5
1
0 -1
-1

Sample Output 

Case 1: 4
Case 2: never





#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define E1 first
#define E2 second

struct Discretization
{
	int nn;
	int val[300000];
	void clear(){nn=0;}
	void Insert(int temp)
	{
		val[++nn]=temp;
	}
	void Sort()
	{
		sort(val+1,val+nn+1);
		int j=1;
		for(int i=2;i<=nn;i++)
			if(val[i]!=val[i-1])
				val[++j]=val[i];
		nn=j;
	}
	int Find(int vv)
	{
		int lo=1,ro=nn;
		while(lo<ro)
		{
			int mid=(lo+ro)/2;
			if(val[mid]<vv)
				lo=mid+1;
			else
				ro=mid;
		}
		return lo;
	}
}D;

int N,X[100009],Y[100009];
int L[1200000],R[1200000],Mid[1200000],MinV[1200000],IncV[1200000];

void Build(int x,int l,int r)
{
	L[x]=l;R[x]=r;Mid[x]=(l+r)/2;
	MinV[x]=0;IncV[x]=0;
	if(l==r)
		return;
	Build(x*2+0,l,Mid[x]);
	Build(x*2+1,Mid[x]+1,r);
}

void Adjust(int x)
{
	MinV[x*2+0]+=IncV[x];
	MinV[x*2+1]+=IncV[x];
	IncV[x*2+0]+=IncV[x];
	IncV[x*2+1]+=IncV[x];
	IncV[x]=0;
}

void LazyChange(int x,int l,int r,int d)
{
	if(L[x]==l&&r==R[x])
	{
		IncV[x]+=d;
		MinV[x]+=d;
		return;
	}
	Adjust(x);
	if(r<=Mid[x])
		LazyChange(x*2+0,l,r,d);
	else if(l>Mid[x])
		LazyChange(x*2+1,l,r,d);
	else
	{
		LazyChange(x*2+0,l,Mid[x],d);
		LazyChange(x*2+1,Mid[x]+1,r,d);
	}
	MinV[x]=min(MinV[x*2+0],MinV[x*2+1]);
}

int FindMin(int x,int l,int r)
{
	if(L[x]==l&&r==R[x])
		return MinV[x];
	Adjust(x);
	if(r<=Mid[x])
		return FindMin(x*2+0,l,r);
	else if(l>Mid[x])
		return FindMin(x*2+1,l,r);
	return min(FindMin(x*2+0,l,Mid[x]),FindMin(x*2+1,Mid[x]+1,r));
}

bool Escape(int dis)
{
	D.clear();
	for(int i=1;i<=N;i++)
		D.Insert(Y[i]-dis);
	for(int i=1;i<=N;i++)
		D.Insert(Y[i]+dis+1);
	D.Insert(-dis);
	D.Insert(+dis+1);
	D.Sort();
	vector<pair< pair<int,int>,pair<int,int>>> Change;
	Change.clear();
	vector<int>Ask;
	Ask.clear();
	for(int i=1;i<=N;i++)
		Change.PB(MP(MP(X[i]-dis,+1), MP(D.Find(Y[i]-dis), D.Find(Y[i]+dis+1)-1)));
	for(int i=1;i<=N;i++)
		Change.PB(MP(MP(X[i]+dis+1,-1), MP(D.Find(Y[i]-dis), D.Find(Y[i]+dis+1)-1)));
	for(int i=1;i<=N;i++)
	{
		if(X[i]-dis>=-dis && X[i]-dis<=+dis)
			Ask.PB(X[i]-dis);
		if(X[i]+dis+1>=-dis && X[i]+dis+1<=+dis)
			Ask.PB(X[i]+dis+1);
	}
	Ask.PB(-dis);
	sort(Change.begin(),Change.end());
	sort(Ask.begin(),Ask.end());
	Ask.erase(unique(Ask.begin(),Ask.end()),Ask.end());
	int t_Change=0;
	Build(1,1,D.nn-1);
	for(int i=0;i<Ask.size();i++)
	{
		while(t_Change<Change.size()&&Change[t_Change].E1.E1<=Ask[i])
		{
			LazyChange(1,Change[t_Change].E2.E1,Change[t_Change].E2.E2,Change[t_Change].E1.E2);
			t_Change++;
		}
		if(Ask[i]>=-dis&&Ask[i]<=+dis)
		{
			if(FindMin(1,D.Find(-dis),D.Find(dis+1)-1)==0)
				return true;
		}
	}
}

void Solve()
{
	int L=1,R=1000001;
	while(L<R)
	{
		int Mid=(L+R)/2;
		if(Escape(Mid))
			L=Mid+1;
		else
			R=Mid;
	}
	if(L==1000001)
		puts("never");
	else
		printf("%d\n",L);
}

int main()
{
	int Case=0;
	while(cin>>N&&N!=-1)
	{
		for(int i=1;i<=N;i++)
			scanf("%d %d",&X[i],&Y[i]);
		printf("Case %d: ",++Case);
		Solve();
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值