【BZOJ1821】[JSOI2010]部落划分(二分,并查集)

题面

BZOJ

洛谷

题解

二分答案,把距离小于二分值的点全部并起来,\(\mbox{check}\)一下是否有超过\(K\)个集合就好了。

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define MAX 1010
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int f[MAX],x[MAX],y[MAX],n,K;
double Sqr(double x){return x*x;}
double Dis(int i,int j){return sqrt(Sqr(x[i]-x[j])+Sqr(y[i]-y[j]));}
int getf(int x){return x==f[x]?x:f[x]=getf(f[x]);}
bool check(double mid)
{
for(int i=1;i<=n;++i)f[i]=i;
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n;++j)
if(Dis(i,j)<=mid)f[getf(j)]=getf(i);
int tot=0;
for(int i=1;i<=n;++i)if(i==getf(i))++tot;
return tot>=K;
}
int main()
{
n=read();K=read();
for(int i=1;i<=n;++i)x[i]=read(),y[i]=read();
double l=0,r=2e4;
while(r-l>1e-4)
{
double mid=(l+r)/2;
if(check(mid))l=mid;
else r=mid;
}
printf("%.2lf\n",l);
return 0;
}

最新文章

  1. Python之Web前端jQuery扩展
  2. [Animatable Properties](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/AnimatableProperties/AnimatableProperties.html)
  3. SQL归档
  4. php内核探索 [转]
  5. 用JSON.parse和eval出现的问题
  6. [codevs1157]2^k进制数
  7. 给input的按钮控件添加onserverclick事件
  8. iOS 6编程Cookbook(影印版)
  9. U大师装系统
  10. C# Windows - ListBox&amp;CheckedListBox
  11. JQuery 实现鼠标经过图片高亮显示,其余图片变暗
  12. 对[yield]的浅究到发现[async][await]
  13. codefirst mvc Self referencing loop detected for property
  14. 关于binary log那些事——认真码了好长一篇
  15. Jenkins : 邮件通知
  16. #云栖大会# 移动安全专场——APP加固新方向(演讲速记)
  17. nyoj886 取石子(八) 威佐夫博弈
  18. 架构之微服务(zookeeper)
  19. AT987 高橋君
  20. Windows Update第三方工具概览

热门文章

  1. excel的宏与VBA入门——代码调试
  2. Visual Studio2017 数据库架构比较
  3. keycloak 调研资料
  4. Linux服务器更换主板后,网卡识别失败的处理方法
  5. Linux系统下本地yum镜像源环境部署-完整记录
  6. ansible环境部署及常用模块总结 - 运维笔记
  7. Net-SNMP V3协议 安装配置笔记(CentOS 6.3/5.6)
  8. vue全局 关键字搜索 v-search
  9. “耐撕团队”部署并测试onezero团队记帐本项目
  10. Linux内核及分析 第七周 可执行程序的装载