跳到正文

题解

题解:P3099 [USACO14JAN] Cow Curling G

Lywh_ddAC约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年3月28日
收录
2026年9月30日
更新
2026年11月21日
篇幅
约 8 分钟
获赞
3
题目
P3099

版本

  1. v1当前

题意转化

题目要求统计“被对手任意三个石头构成的三角形包含”的石头数量。观察到一个关键结论:

一个点能被点集 SS 中的三个点构成的三角形包含(含边界),当且仅当该点在点集 SS 的凸包内部(含边界)。

因此,问题可以简化为:

  1. 分别求出队伍 A 和队伍 B 的凸包;
  2. 统计队伍 B 中在 A 凸包内的点数(A 队得分);
  3. 统计队伍 A 中在 B 凸包内的点数(B 队得分)。

二分判断点是否在凸包内

对于一个点,如何快速判断它是否在凸包内?假设我们有一个凸包和待判断的点 optopt,如下图所示:

凸包判断示例1

首先,判断点 optopt 是否在 A1A7→\overrightarrow{A_1A_7} 和 A1A2→\overrightarrow{A_1A_2} 之间(即 A1A7→\overrightarrow{A_1A_7} 顺时针旋转可到 A1opt→\overrightarrow{A_1opt},A1A2→\overrightarrow{A_1A_2} 逆时针旋转可到 A1opt→\overrightarrow{A_1opt})。这一步可以通过叉积实现:若 A1A2→×A1opt→<0\overrightarrow{A_1A_2} \times \overrightarrow{A_1opt} < 0 或 A1A7→×A1opt→>0\overrightarrow{A_1A_7} \times \overrightarrow{A_1opt} > 0,则点在凸包外。

C++
if(side(st[1],st[2],a[i])<0||side(st[1],st[m],a[i])>0)	continue ;

接下来,使用二分查找找到第一条在点 optopt 上方的边 A1A6→\overrightarrow{A_1A_6},此时点 optopt 一定在 A1A6→\overrightarrow{A_1A_6} 和 A1A5→\overrightarrow{A_1A_5} 之间(这一单调性显然:若点不在较高的边下方,则一定不在较低的边下方):

C++
int l=2,r=m,an=0;
while(l<=r){// 找第一条在a_i上方的边
	int mid=(l+r)/2;
	if(side(st[1],st[mid],a[i])<=0){// 边在点上方
		an=mid;
		r=mid-1;
	}
	else{
		l=mid+1;
	}
}

最后,判断 A6A5→×A6opt→≤0\overrightarrow{A_6A_5} \times \overrightarrow{A_6opt} \leq 0 是否成立,若成立则点在凸包内,否则在凸包外,如下图所示:

凸包判断示例2

对应的代码如下:

C++
if(side(st[an],st[an-1],a[i])<=0)
	ans++;

特殊情况处理

  • 凸包退化为单个点:只有当待判断点与该点重合时,才算被包含:
C++
if(m==1){
	for(int i=1;i<=n;i++)
		if(a[i].x==st[1].x&&a[i].y==st[1].y)
			ans++;
	return ans;
}
  • 凸包退化为线段:待判断点需共线且在线段区间内:
C++
if(m==2){
	for(int i=1;i<=n;i++)
		if(side(st[1],st[2],a[i])==0&&dis(st[1],st[2])>=dis(st[1],a[i])&&dis(st[1],st[2])>=dis(st[2],a[i]))
			ans++;
	return ans;
}

参考代码

C++
#include<bits/stdc++.h>
using namespace std;
#define N 50005
#define int long long
bool MT;

int ans,n,ma,mb;

struct L{
	int x,y;
}a[N*4],b[N],st1[N*4],st2[N*4],opt;

int cmp(L a,L b){
	if(a.x==b.x)	return a.y<b.y;
	return a.x<b.x;
}

// 计算向量叉积:A × B
int cross(L A,L B){
	return A.x*B.y-A.y*B.x;
} 

// 计算 (b - a) × (c - a),判断点c在直线ab的哪一侧
// 返回值 > 0:c在左侧;< 0:c在右侧;= 0:共线
int side(L a,L b,L c){
	L A={b.x-a.x,b.y-a.y};// a->b
	L B={c.x-a.x,c.y-a.y};// a->c
	return cross(A,B);
}

// 计算两点间距离
double dis(L a,L b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int top=0;
// Andrew算法求凸包,结果存于st数组,返回凸包大小(含重复的起点)
void Andrew(L a[],L *st){
	sort(a+1,a+1+n,cmp);
	top=0;
	for(int i=1;i<=n;i++){
		while(top>1&&side(st[top-1],st[top],a[i])<=0)
			top--;
		st[++top]=a[i];
	}
	int t=top;
	for(int i=n-1;i>=1;i--){
		while(top>t&&side(st[top-1],st[top],a[i])<=0)
			top--;
		st[++top]=a[i];
	}
}

// 统计点集a中在凸包st内的点数,m为凸包大小(不含重复起点)
int check(L st[],L a[],int m){
	int ans=0;
	if(m==0)	return 0;
	// 凸包为单个点
	if(m==1){
		for(int i=1;i<=n;i++)
			if(a[i].x==st[1].x&&a[i].y==st[1].y)
				ans++;
		return ans;
	}
	// 凸包为线段
	if(m==2){
		for(int i=1;i<=n;i++)
			if(side(st[1],st[2],a[i])==0&&dis(st[1],st[2])>=dis(st[1],a[i])&&dis(st[1],st[2])>=dis(st[2],a[i]))
				ans++;
		return ans;
	}
	// 凸包为多边形
	for(int i=1;i<=n;i++){
		// 先判断是否在凸包的“扇形”外
		if(side(st[1],st[2],a[i])<0||side(st[1],st[m],a[i])>0)	continue ;
		// 二分找到第一条在点上方的边
		int l=2,r=m,an=0;
		while(l<=r){
			int mid=(l+r)/2;
			if(side(st[1],st[mid],a[i])<=0){
				an=mid;
				r=mid-1;
			}
			else{
				l=mid+1;
			}
		}
		// 判断是否在凸包内
		if(side(st[an],st[an-1],a[i])<=0)
			ans++;
	}
	return ans;
}

bool MS;

signed main(){
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i].x>>a[i].y; 
	Andrew(a,st1);
	ma=top;

	for(int i=1;i<=n;i++)
		cin>>b[i].x>>b[i].y;
	Andrew(b,st2);
	mb=top;

	// 注意:Andrew算法返回的凸包包含重复的起点(st[top] = st[1]),因此传参时用ma-1和mb-1
	cout<<check(st1,b,ma-1)<<" "<<check(st2,a,mb-1);
	cerr<<endl<<(&MS-&MT)/1024.0/1024.0<<"MB "<<1.0*clock()/CLOCKS_PER_SEC<<" ms\n"; 
	return 0;
}
原文
存档评论6 条抓取于