这题好像不用long long特判也能过?
查看原帖
这题好像不用long long特判也能过?
1078603
owenzhuang楼主2025/8/4 20:25

在这一篇有特判的好题解其中有

if ((x[j]-x[k])*(x[j]-x[k])+(y[j]-y[k])*(y[j]-y[k])>4*r*r) continue;
            	//防止爆long long的特判。

但是我没有特判也能AC? 我的代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;

int num_of_holes[1005],sz[1005];
int x[1005],y[1005],z[1005];
int holes_at_bottom[1005],holes_at_top[1005];
int num_bottom,num_top;
int find(int x){
	if(num_of_holes[x]==x) return x;
	return num_of_holes[x]=find(num_of_holes[x]);
}

void join(int c1,int c2){
	int f1=find(c1),f2=find(c2);
	if(f1 != f2){
		if(sz[f1]>sz[f2]) swap(f1,f2);
		num_of_holes[f1] = f2;
		sz[f2]+= sz[f1];
		if(sz[f1]==sz[f2]) sz[f2]++;
	}
}

int dis(int x,int y,int z,int x1,int y1,int z1){
    return (x-x1)*(x-x1)+(y-y1)*(y-y1)+(z-z1)*(z-z1);
}

signed main(){
	
	int T,n,h,r;
	cin>>T;
	while(T--){
		num_bottom=0,num_top=0;
		cin>>n>>h>>r;
		for(int i=1;i<=n;i++){
			num_of_holes[i] = i;
			sz[i]=1;
		}
		for(int i=1;i<=n;i++){
			cin>>x[i]>>y[i]>>z[i];
			if(z[i]+r>=h){
				num_top++;
				holes_at_top[num_top]=i;
			}
			if(z[i]-r<=0){
				num_bottom++;
				holes_at_bottom[num_bottom]=i;
			}
		} 
		for(int i=2;i<=n;i++){
			for(int j=1;j<=i;j++){
				if(dis(x[i],y[i],z[i],x[j],y[j],z[j]) <= 4*r*r){
					join(num_of_holes[i],num_of_holes[j]);
				}
			}
		}
		bool flag=false;
		for(int i=1;i<=num_top;i++){
			for(int j=1;j<=num_bottom;j++){
				if(find(num_of_holes[holes_at_top[i]])==find(num_of_holes[holes_at_bottom[j]])){
					flag=true;
					break;
				}
			}
			if(flag==true) break;
		}
		if(flag==true) cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}
//   \o/

顺便问一下大佬们有优化的方法吗?

2025/8/4 20:25
加载中...