比赛 20110412pm 评测结果 AAAWWWWWAA
题目名称 拯救奶牛贝希 最终得分 50
用户昵称 Citron酱 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2011-04-12 16:52:16
显示代码纯文本
#include <fstream>
#include <cstdlib>
#include <climits>

#define I_F "rescuea.in"
#define O_F "rescuea.out"
#define MAXm 10000

using namespace std;

struct point
{
	long x,y;
};

long n,m;
point outs[MAXm];
point b,ansp;
long ans=LONG_MAX;

void Input();
inline bool Compare(point,point);
inline void Swap(point&,point&);
void Qsort(long,long);
inline long Abs(long);
inline long Len(point,point);
inline bool Odd(long);
inline point Left(point);
void Search();
void Output();

int main()
{
	Input();
	Qsort(0,m-1);
	Search();
	Output();
	return 0;
}

void Input()
{
	ifstream fin(I_F);
	fin>>n>>m;
	fin>>b.x>>b.y;
	for (long i=0; i<m; fin>>outs[i].x>>outs[i].y, i++);
	fin.close();
}

inline bool Compare(point a, point b)
{
	if (a.x<b.x)
		return true;
	if (a.x>b.x)
		return false;
	if (a.y<b.y)
		return true;
	return false;
}

inline void Swap(point &a, point &b)
{
	point t=a;
	a=b;
	b=t;
}

void Qsort(long l, long r)
{
	point x=outs[l+rand()%(r-l+1)];
	long i=l, j=r;
	do
	{
		while (Compare(x,outs[i])) i++;
		while (Compare(outs[j],x)) j--;
		if (i<=j)
			Swap(outs[i++],outs[j--]);
	} while (i<j);
	if (i<r) Qsort(i,r);
	if (l<j) Qsort(l,j);
}

inline long Abs(long x)
{
	return (x<0)?(-x):x;
}

inline long Len(point a, point b)
{
	return (Abs(a.y-b.y)-(((a.x-b.x)*(a.y-b.y)<0)?0:2)+(2*Abs(a.x-b.x)));
}

inline bool Odd(long x)
{
	return (x%2>0);
}

inline point Left(point x)
{
	point t=x;
	t.y--;
	return t;
}

void Search()
{
	long t;
	for (long i=0; i<m; i++)
	{
		if (Odd(outs[i].y) && Odd(b.y))
			t=Len(outs[i],b);
		else if ((!Odd(outs[i].y)) && (!Odd(b.y)))
			t=Len(Left(outs[i]),Left(b));
		else if (Odd(outs[i].y))
			t=Len(outs[i],Left(b))+((outs[i].y>b.y+1)?1:(-1));
		else
			t=Len(Left(outs[i]),b)+((outs[i].y>b.y+1)?(-1):1);
		if (t<ans)
			ans=t,
			ansp=outs[i];
	}
}

void Output()
{
	ofstream fout(O_F);
	fout<<ansp.x<<' '<<ansp.y<<'\n'
		<<ans+1<<'\n';
	fout.close();
}