R 的农场 chebnear (二分答案+最*面点对)

时间:2023-03-09 08:50:10
R 的农场 chebnear (二分答案+最*面点对)

题面

R 的农场 chebnear (二分答案+最*面点对)R 的农场 chebnear (二分答案+最*面点对)R 的农场 chebnear (二分答案+最*面点对)

$ solution: $

这道题想到二分答案应该是不难的,因为题目是求平均工资的最小值,这个显然具备单调性:

我们设平均工资的最小值为ans,如果我们现在的平均工资x小于ans那么将x带入题目中必定会出现有两个守卫在吵架,如果我们现在的平均工资x大于等于ans那么将x带入题目中必定不会出现有两个守卫在吵架。

所以我们现在就要想办法知道在确定了平均工资的情况下,如何判断是否有守卫吵架。首先我们看题:如果两个守卫有共同的朋友,那么他们也会成为朋友。这样我们发现可以用并查集维护这个关系,然后通过并查集看两个守卫是否在同一个朋友圈。接下来我们就要知道在所有距离小于k的两个守卫中是否有一对不在同意朋友圈内。

考场上还不知道如何求,于是卡死在这里了,后来知道这就是平面最近点对,我们用这个方法可以求出所有不在同一朋友圈内的两个守卫的最近距离,然后看是不是小于k,是就说明会发生争吵。

$ code: $

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set> #define ll long long
#define db double
#define inf 0x7fffffff
#define rg register int using namespace std; struct su{
db x,y;int z;
}a[100005],b[100005]; struct pi{
int t1,t2;db v;
}c[200005]; int n,m;
db k;
int s[100005]; inline int qr(){
char ch;
while((ch=getchar())<'0'||ch>'9');
int res=ch^48;
while((ch=getchar())>='0'&&ch<='9')
res=res*10+(ch^48);
return res;
} inline bool cmp_x(su x,su y){return x.x<y.x;}
inline bool cmp_y(su x,su y){return x.y<y.y;}
inline bool cmp_v(pi x,pi y){return x.v<y.v;} inline int get(int x){
if(s[x]==x)return x;
return s[x]=get(s[x]);
} inline bool check(int l,int r){
if(l>=r)return 1;
int mid=(l+r)>>1;
if(!check(l,mid))return 0;
if(!check(mid+1,r))return 0;
while(a[l].x+k<a[mid].x)l++;
while(a[r].x-k>a[mid].x)r--;
int top=0;
for(rg i=l;i<=r;++i)b[++top]=a[i];
sort(b+1,b+top+1,cmp_y);
for(rg i=1;i<=top;++i)
for(rg j=i+1;j<=top;++j)
if(b[j].y-b[i].y>k) break;
else if(fabs(b[j].x-b[i].x)<=k&&get(b[j].z)!=get(b[i].z))return 0;
return 1;
} inline bool yu(int x){
for(rg i=1;i<=n;++i)s[i]=i;
for(rg i=1;i<=x;++i)
s[get(c[i].t1)]=get(c[i].t2);
return check(1,n);
} int main(){
freopen("chebnear.in","r",stdin);
freopen("chebnear.out","w",stdout);
n=qr(),m=qr();scanf("%lf",&k);
for(rg i=1;i<=n;++i)
scanf("%lf%lf",&a[i].x,&a[i].y),a[i].z=i;
sort(a+1,a+n+1,cmp_x);
for(rg i=1;i<=m;++i){
c[i].t1=qr(),c[i].t2=qr();
scanf("%lf",&c[i].v);
}sort(c+1,c+m+1,cmp_v);
int l=0,r=m-1,mid;
while(l<=r){
mid=(l+r)>>1;
if(yu(mid))r=mid-1;
else l=mid+1;
}printf("%.3lf\n",c[l].v);
return 0;
}