HDU多校7 - 6850 Game(思维+贪心)

题目链接:点击查看

题目大意:二维平面上有 n 个点,现在两个人博弈,每个点至多遍历一次,先手从第一个点出发,随意选择一个点作为下一个点,后手再选择一个点,从现在开始需要满足的条件是,接下来选择点的距离必须要大于前一次两点的距离,无法做出选择的人失败

题目分析:挺好的一道博弈题,网上很多博客的证明和思路都是错的,只不过因为数据都是整数的原因,那样实现确实可以 AC,这里直接讲一下官方题解的思路

首先求出 n * ( n - 1 ) / 2 条边作为两两点对的距离,贪心排序一下,按照距离从大到小分类讨论,对于每个距离 dis 来说,如果 dis 下存在的点对 ( x , y ) 都未被标记过,则加入到 dis 下的集合(此处的集合指的是点集,下文同)中,且将其设置标记,最后如果点 1 被标记了答案就是 yes,反之为 no

博弈策略如下,首先不难看出,至多只有一个点会没有被标记,假如有两个点未被标记,那么这两个点构成的点对可以加入到其距离 dis 的集合中去,所以当未标记的点大于一个时,完全可以两两互相组合,使得至多只有一个点落单

而且还有一点需要看出的是,这个未被标记的点,与其他点的相对距离一定是比较小的(如果距离大的话,在前面贪心的过程中就不会落单了)

其次因为上述实现,每次集合中加入的是一个点对 ( x , y ) ,所以每个集合中至少有两个元素,基于此,如果现在起点在点 p ,如果点 p 属于集合 s 中,则可以先手选择 s 中的除了点 p 外的任意一个点(因为每个集合中的点对距离都是相同的),此时后手只能跨集合选择,先手依然选择新的集合中的另外一个点,如此往复,先手必胜

如果点 p 不属于任何一个集合的话,那么先手无论选择哪个点,肯定都会对应着一个集合,后手只需要按照必胜策略选择就好了,这样先手就必败

用了两个数组模拟集合和标记的运算

代码:
 

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
 
typedef long long LL;
 
typedef unsigned long long ull;
 
const int inf=0x3f3f3f3f;
 
const int N=2e3+100;

struct Edge
{
	int x,y;
	LL dis;
	bool operator<(const Edge& t)const
	{
		return dis>t.dis;
	}
}edge[N*N];

struct Point
{
	LL x,y;
	void input()
	{
		scanf("%lld%lld",&x,&y);
	}
	LL dis(Point& t)
	{
		return (t.x-x)*(t.x-x)+(t.y-y)*(t.y-y);
	}
}point[N];

bool sg[N],vis[N];

int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);
	int w;
	cin>>w;
	while(w--)
	{
		int n,cnt=0;
		scanf("%d",&n);
		memset(vis,false,n+5);
		memset(sg,false,n+5);
		for(int i=1;i<=n;i++)
			point[i].input();
		for(int i=1;i<=n;i++)
			for(int j=i+1;j<=n;j++)
				edge[++cnt].x=i,edge[cnt].y=j,edge[cnt].dis=point[i].dis(point[j]);
		sort(edge+1,edge+1+cnt);
		int r=1;
		while(r<=cnt)
		{
			int l=r;
			while(edge[r].dis==edge[l].dis)
				r++;
			for(int i=l;i

 

你可能感兴趣的:(博弈,贪心)