排位赛2-Fence Planning

题目

Farmer John’s N cows, conveniently numbered 1…N (2≤N≤105), have a complex social structure revolving around “moo networks” — smaller groups of cows that communicate within their group but not with other groups. Each cow is situated at a distinct (x,y) location on the 2D map of the farm, and we know that M pairs of cows (1≤M<105) moo at each-other. Two cows that moo at each-other belong to the same moo network.

In an effort to update his farm, Farmer John wants to build a rectangular fence, with its edges parallel to the x and y axes. Farmer John wants to make sure that at least one moo network is completely enclosed by the fence (cows on the boundary of the rectangle count as being enclosed). Please help Farmer John determine the smallest possible perimeter of a fence that satisfies this requirement. It is possible for this fence to have zero width or zero height.

题意

在二维网格上有n头牛,他们之间有n对关系,现在需要建造栅栏,建造得栅栏一定要把有联系的牛都围起来,求最小面积的栅栏面积是多少。

解法

先用并查集将所有牛分好类,然后直接求每一个关系圈的牛所需要的栅栏面积即可。

代码:

#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
const int maxn=200010;
int n,m,xx,yy,ans,father[maxn],max_x[maxn],min_x[maxn],max_y[maxn],min_y[maxn],x[maxn],y[maxn];int getfather(int x)
{if (father[x]!=x) father[x]=getfather(father[x]);return father[x];
}int main()
{scanf("%d%d",&n,&m);for (int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);for (int i=1;i<=n;i++) {father[i]=i;max_x[i]=-1;min_x[i]=100000010;max_y[i]=-1;min_y[i]=100000010;}for (int i=1;i<=m;i++) {scanf("%d%d",&xx,&yy);int fax=getfather(xx);int fay=getfather(yy);if (fax==fay) continue;father[fax]=fay;}for (int i=1;i<=n;i++) {father[i]=getfather(father[i]);int fa=father[i];max_x[fa]=max(max_x[fa],x[i]);min_x[fa]=min(min_x[fa],x[i]);max_y[fa]=max(max_y[fa],y[i]);min_y[fa]=min(min_y[fa],y[i]);}ans=-1;for (int i=1;i<=n;i++) if (father[i]==i) {if (ans==-1) ans=(max_x[i]-min_x[i]+max_y[i]-min_y[i])*2; else ans=min(ans,(max_x[i]-min_x[i]+max_y[i]-min_y[i])*2);}printf("%d",ans);return 0;
} 

排位赛2-Fence Planning相关推荐

  1. 2020 GDUT Rating Contest II (Div. 2) A. Fence Planning

    来源 codeforces 2020 GDUT Rating Contest II (Div. 2) CF链接 题目: Farmer John's N cows, conveniently numbe ...

  2. GDUT_排位赛题解报告_第2场_Fence Planning

    题目: A. Fence Planning time limit per test1 second memory limit per test256 megabytes inputstandard i ...

  3. codeforces 排位赛2

    传送门 题面 A. Fence Planning time limit per test1 second memory limit per test256 megabytes inputstandar ...

  4. APICS与AX的Master Planning(四)---Time Fence时限(时界)

    对于一个计划系统来说,用一个个的时间点来划定不同的范围是必要的,因为不可能要求计划员去时时刻刻关注所有的订单,人的精力毕竟有限,圈定一个范围让计划员只关注某个时间点以内的有限的订单是比较合理的,这一篇 ...

  5. 【Planning】R3 PTF and firm mapped with APO

    Purpose: 理清R3里的firm type,与APO里的SNP production horizon.ST horizon等的关系. Approach: MRP Type P1 - > C ...

  6. MRP:时间栅栏(Time Fence)

    一,定义 时间栅栏是一个物料的属性.它将整个计划覆盖的日期(即从今天Today至计划期间Planning Horizontal)范围分成两段,分别为"时间栅栏内"(Inside t ...

  7. Apollo Planning学习(9)-------速度规划

    顺着之前学习public road planner 的路径规划中lane follow的task,在得到的规划路径上再进行速度规划.大致思路为先利用ST Graph,将障碍物.限速等投影在ST图上,利 ...

  8. 极限编程 (Extreme Programming) - 迭代计划 (Iterative Planning)

    (Source: XP - Iteration Planning) 在每次迭代开始时调用迭代计划会议,以生成该迭代的编程任务计划.每次迭代为1到3周. 客户从发布计划中按照对客户最有价值的顺序选择用户 ...

  9. ARP(Accounting Resource Planning)项目感想

    ARP是Accounting Resource Planning(会计资源计划)的简称. 转载于:https://blog.51cto.com/lya041/690079

最新文章

  1. 整合spring cloud云架构 - SSO单点登录之OAuth2.0登录认证(1)
  2. 信息网络基础设施普遍薄弱,提防信息安全风险--央行副行长
  3. 5G时代,MPLS WAN如何发展?-vecloud服务器
  4. 【计算广告】不懂这些专业术语别说你懂广告
  5. metamask中的import account的代码实现
  6. 从源码层面带你实现一个自动注入注解
  7. 计算机学业水平测试题及答案初中,初中信息技术学业水平考试试题30号试题.doc...
  8. Java基础学习总结(86)——Java异常处理机制Exception抛出异常时throw和throws用法详解
  9. 注册表的监控 -- WIN9X
  10. 流量转发的思路-软件流量转发 管家婆 客户端 端口 更改
  11. SQL Server2012版完全卸载
  12. 网络流基础、最大流最小割定理以及证明
  13. 戴尔dell poweredge r730服务器系统安装以及raid配置详解教程
  14. 磁盘是随机存储设备,但不是随机存储器(RAM)。为什么?
  15. ssm毕设项目基于的少儿编程学习系统2lsiy(java+VUE+Mybatis+Maven+Mysql+sprnig)
  16. STM32G070RBT6迷你开发板制作总结
  17. 600度近视眼恢复方法_高度近视600度如何恢复视力?这三大办法
  18. 远程删除用户手机照片?拼多多回应
  19. 【JS】Javascript中的this到底是什么
  20. 不起眼的浏览器_借助不起眼的独立游戏包,以低廉的价格获得令人敬畏的游戏...

热门文章

  1. 永生生物_指导:通往永生的道路
  2. 博恩崔西--时间管理
  3. 也发个网通客户端Dr.COM的破解版
  4. 多线程_锁(八股文)
  5. 【SDOI 2016】征途
  6. C语言-成绩排名(结构)
  7. 怎么判断Linux是否适合你?
  8. 移动互联网将成为过去式,物联网时代已经来临?
  9. 定制table2excel插件,支持自定义列,导出excel
  10. RT-Thread系统简介