[CF851C]Five Dimensional Points

280 篇文章 1 订阅

题目

传送门 to VJ

传送门 to CF

题目概要
对于 n ( n ≤ 1 0 3 ) n(n\le 10^3) n(n103) 5 5 5 维空间中的点,假如存在三个不同的点 A , B , C A,B,C A,B,C 满足 ∠ A B C < π 2 \angle ABC<\frac{\pi}{2} ABC<2π ,则 B B B 点是 “不好的” 。 ∠ A B C \angle ABC ABC 即向量 A B AB AB 和向量 C B CB CB 的夹角。其余弦是向量点积除以二者的长度之积。用反三角求出该角。

输出所有 “好的” 点。

思路

这个角度计算公式并不是随便写出来的。这是在 n n n 维空间中两个向量的夹角。

因为点积的定义,就是模长相乘,再乘上夹角的余弦。

思路一

感谢@NKtif 的博客@Difstpoftf 的博客提供了本思路。

首先,我们有一个粗略的判断,在同一个象限中的点,和原点形成的角是锐角。在 c c c 维空间中,象限只有 2 c 2^c 2c 个,根据鸽巢原理,只要超过了 2 c + 1 2^c+1 2c+1 个点,必死。

更细致一点,我们发现,从某一个点出发,其实只有 2 c 2c 2c 个互相垂直的射线。这些射线上,每一条可以选一个点,所以,超过 2 c + 1 2c+1 2c+1 个点时,必死。

小数据(即 n ≤ 2 c + 1 n\le 2c+1 n2c+1 时)暴力即可。计算一次点积为 O ( c ) \mathcal O(c) O(c) ,时间复杂度 O ( c 4 ) \mathcal O(c^4) O(c4) (实际上跑不满)。

思路二

三个点会形成一个三角形(即使共线,下面的论述仍然成立),所以,三个角中,最少都会有两个锐角。这同时也说明:只要 n > 2 n>2 n>2 ,那么最多只有一个好点。

那么,我们枚举三个顶点,并且这三个顶点都没有被认定为坏点。可以看出,每次至少都会排除两个点。直到只剩下不到两个点是不坏的点。

找不坏的点,可以用链表实现。所以这一部分的复杂度是 O ( c n ) \mathcal O(cn) O(cn) 的。

剩下的两个点,随便再找一个点作为搭档,检测一次。现在只剩下一个点没有被排除了。

然而得暴力枚举另外两个点。但是,这是 O ( n 2 ) \mathcal O(n^2) O(n2) 的吗?

根据 思路一 ,真正满足与其他所有点都恰好垂直的,只有 2 c 2c 2c 个点。一旦检测到,这个点成为了坏点,立刻停止。所以点对的数量为 O [ min ⁡ ( c n , n 2 ) ] \mathcal O[\min(cn,n^2)] O[min(cn,n2)] ,复杂度就为 O [ min ⁡ ( c 2 n , c n 2 ) ] \mathcal O[\min(c^2 n,cn^2)] O[min(c2n,cn2)]

综上所述,本方法的复杂度是 O [ c n min ⁡ ( c , n ) ] \mathcal O[cn\min(c,n)] O[cnmin(c,n)] 的。

思路三

思路一思路二一起用就可以了。复杂度变为 O ( c 3 ) \mathcal O(c^3) O(c3)

代码

提供思路二的代码,作为参考(因为加入思路一的剪枝是很简单的)。

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline int readint(){
	int a = 0; char c = getchar(), f = 1;
	for(; c<'0' or c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c and c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}
inline void writeint(long long x){
	if(x < 0) putchar('-'), x = -x;
	if(x > 9) writeint(x/10);
	putchar((x%10)^48);
}
# define MB template < class T >
MB void getMax(T &a,const T &b){ if(a < b) a = b; }
MB void getMin(T &a,const T &b){ if(b < a) a = b; }

struct Point{
	int l[5];
	void input(){
		for(int i=0; i<5; ++i)
			l[i] = readint();
	}
	Point operator - (const Point &that) const {
		Point p;
		for(int i=0; i<5; ++i)
			p.l[i] = l[i]-that.l[i];
		return p;
	}
	int operator * (const Point &that) const {
		int res = 0;
		for(int i=0; i<5; ++i)
			res += l[i]*that.l[i];
		return res;
	}
};
double langle(const Point &a,const Point &b){
	return acos((a*b)/sqrt(a*a)/sqrt(b*b));
}

struct Node{
	Node* nxt; int x;
	Node(int val):x(val){ nxt = nullptr; }
	operator int(){ return x; }
};
Node* head;

const int MaxN = 1005;
const double PI = acos(-1);
Point p[MaxN]; bool bad[MaxN];

void check(int a,int b,int c){
	double theta = langle(p[b]-p[a],p[c]-p[a]);
	if(theta < PI/2) bad[a] = true;
}

# define Continue(x) { x = x->nxt; continue; }
// Continue(x->nxt)将x->nxt从链表中删除;Continue(x)循环的下一轮
int main(){
	int n = readint();
	for(int i=1; i<=n; ++i) p[i].input();
	auto tail = (head = new Node(-1));
	for(int i=1; i<=n; ++i) // 初始化链表
		tail = (tail->nxt = new Node(i));
	# define i (I->nxt) // 一号顶点
	for(auto I=head; i!=nullptr; ){
		# define j (J->nxt) // 检测点二号
		for(auto J=i; j!=nullptr and not bad[*i]; ){
			# define k (K->nxt) // 监测点三号
			for(auto K=j; k!=nullptr and not bad[*i] and not bad[*j]; ){
				check(*i,*j,*k), check(*j,*i,*k), check(*k,*i,*j);
				if(bad[*k]) Continue(k); /* else */ Continue(K);
			}
			# undef k // 别忘了取消宏...
			if(bad[*j]) Continue(j); /* else */ Continue(J);
		}
		# undef j // ...不然你就没有循环变量可用了...
		if(bad[*i]) Continue(i); /* else */ Continue(I);
	}
	# undef i // ...死得很惨
	int calc = 0;
	for(int i=1; i<=n; ++i)
		if(not bad[i]) ++ calc;
	if(calc == 2){
		int a = *(head->nxt), b = *(head->nxt->nxt);
		for(int i=0; i<n and (i==0 or i==a or i==b); ++i)
			if(i+1 != a and i+1 != b){
				check(a,b,i+1); if(bad[a]) -- calc;
				check(b,a,i+1); if(bad[b]) -- calc;
			}
	}
	if(calc == 1){
		int survivor;
		for(int i=0; i<n and (i==0 or bad[i]); ++i)
			if(not bad[i+1]) survivor = i+1;
		for(int i=1; i<=n and not bad[survivor]; ++i)
			for(int j=i+1; j<=n and not bad[survivor]; ++j)
				check(survivor,i,j); // 配合剪枝
		if(bad[survivor]) -- calc; // 变馊了
	}
	printf("%d\n",calc);
	for(int i=1; i<=n; ++i)
		if(not bad[i])
			printf("%d\n",i);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值