跳到正文

题解

题解:P13667 [GCPC 2023] Balloon Darts

枫原万叶约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年8月10日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 2 分钟
获赞
1
题目
P13667

版本

  1. v1当前

刚开始 老师 也没看出来,他给出的思路是把一个点以一根线旋转 180 度,那么就枚举这样的范围中哪个点的情况是最优的,但是这个思路时间复杂度过高(其实是我精度调不出来),下面来看一下正解。

考虑一下朴素的搜索是先以此确定三条直线,然后看看是否能穿过所有点,但是这样的做法时间复杂度达到 O(n3)O(n^3) 显然是不够优秀的,这个时候你可以看看展开标签中的第一个标签,考虑用鸽巢原理去优化。

因为对于 nn 个点放在 33 条直线上,如果有解,那么 44 个点一定有 22 个在同一个直线上,那只需要找前 44 个点组成的所有直线即可。

总结一下就是,对于平面上的任意 (k+2)(k+2) 个点,如果不能用k条直线覆盖,那么至少存在 (k+1)(k+1) 个点共线。

代码很好写,这里直接附上。

C++
#include<bits/stdc++.h>
#define endl "\n"
#define Robin ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;

typedef long long LL;

struct P {
	LL x, y;
	P(LL x=0, LL y=0) : x(x), y(y) {}
};

bool col(P a, P b, P c) {
	return (b.x-a.x)*(c.y-a.y) == (b.y-a.y)*(c.x-a.x);
}

bool f(vector<P> p, int k) {
	int n=p.size();
	if(n<=k) return 1;
	if(!k) return 0;
	if(k==1) {
		for(int i=2;i<n;i++)
			if(!col(p[0],p[1],p[i])) return 0;
		return 1;
	}
	int m=min(n,k+1);
	for(int i=0;i<m;i++)
		for(int j=i+1;j<m;j++) {
			vector<P> t;
			for(int x=0;x<n;x++)
				if(!col(p[i],p[j],p[x])) t.push_back(p[x]);
			if(f(t,k-1)) return 1;
		}
	return 0;
}

int main() {
	int n; cin>>n;
	vector<P> p(n);
	for(int i=0;i<n;i++) cin>>p[i].x>>p[i].y;
	cout<<(f(p,3)?"possible":"impossible")<<endl;
	return 0;
} 
原文