传送门

题面:

J. The Volcano Eruption

time limit per test

5.0 s

memory limit per test

64 MB

input

standard input

output

standard output

"The volcano erupted!". Shouted Shahhoud, as he was attempting to wake Saeed up.

"You need to save Noor, he is trapped in the lab next to the volcano!". Shahhoud said.

You can picture the road between the base and the lab as a rectangle whose bottom left corner is at (0, 0) and top right corner is at (W, L). There are n holes filled with lava from the volcano. The ith hole is a circle whose center is at coordinates (xi, yi) and has a radius of ri. The holes may intersect to make larger holes.

Saeed Starts his mission at any point on the bottom side of the rectangle ( any point (0...W, 0)), and has to reach any point on the top side of the rectangle ( any point (0...W, L)), in order to save Noor.

None of the holes intersect with the bottom or the top sides of the rectangle ( the starting and finishing line).

Saeed can take special suits in his anti-lava backpack. Each special suit allows him to swim in one or more intersecting holes (once he goes out of the lava onto the street, he has to throw away the suit). Saeed must stay inside the street at all times. Please note that if two holes are touching each other by the border, then Saeed can't walk from between them without wearing a special suit.

What is the minimum number of suits Saeed has to take with him in order to reach the lab and save Noor?

Input

The first line contains a single integer T, the number of test cases. The first line of each test case contains 3 integers N,W, and L. The number of holes, the width, and the length of the rectangle. (1 ≤ N ≤ 1000) (1 ≤ W, L ≤ 109) Each of the following N lines contain 3 integers (0 ≤ xi ≤ W), (0 < yi < L), and (1 ≤ ri ≤ 109), the coordinates and radius of the ith hole. It's guaranteed that none of the holes intersect with the bottom or the top sides of the rectangle ( the starting and finishing line).

Output

For each test case, output a single line containing a single integer, the minimum number of suits Saeed must take with him to save Noor.

Example

input

Copy

1
5 5 18
4 2 1
2 12 3
4 6 2
2 6 1
1 7 1

output

Copy

2

Note

The figure below shows a possible solution for the sample.

题意:

有一个长度为L,宽度为W的长方形区间。一共有n个圆,这n个圆可能两两相交,当你进入圆的区域的时候,你需要换一次衣服;而当你走出圆的领域的时候,你也需要换一次衣服。现在问你,在不能走出长方形区间的前提下,你最少需要换多少次衣服。

题目分析:

拿到题目一看,貌似需要处理一堆圆相交的问题,貌似处理起来非常困难。

但是如果我们再深入观察的话,我们可以发现,对于一个圆,倘若它恰好在长方形区间内部(与长方形没有交点),则我们可以从两边绕过这块区间。因此,当且仅当存在一个圆,它的最左端覆盖掉长方形左边,最右端覆盖掉长方形右边,此时,我们必须经过该圆。因此我们只需要处理出左右边界的情况即可。

而因为可能存在多个圆相交的情况,此时,我们可以将相交的圆看成一个整体,用并查集对这一块区间进行维护,最后处理一下左右边界情况统计答案即可。

代码:

#include <bits/stdc++.h>
#define maxn 1005
using namespace std;
typedef long long ll;
struct Node{ll x,y,r;
}q[maxn];
bool Intertion(Node a,Node b){ll tmp=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);ll rr=(a.r+b.r)*(a.r+b.r);if(tmp>rr) return 0;else return 1;
}
int Far[maxn];
int Find_F(int x){//并查集if(Far[x]==x) return x;else return Far[x]=Find_F(Far[x]);
}
void unite(int x,int y){x=Find_F(x),y=Find_F(y);if(x==y) return ;else Far[x]=y;
}
int Left[maxn],Right[maxn];//左边界和右边界的情况
void init(int n){for(int i=0;i<=n;i++){Far[i]=i;Left[i]=Right[i]=0;}
}
int main()
{int t;scanf("%d",&t);while(t--){ll n,w,l;scanf("%lld%lld%lld",&n,&w,&l);init(n);for(int i=0;i<n;i++){scanf("%lld%lld%lld",&q[i].x,&q[i].y,&q[i].r);}for(int i=0;i<n;i++){for(int j=0;j<i;j++){//用并查集去维护相交的圆if(Intertion(q[i],q[j])) unite(i,j);}}for(int i=0;i<n;i++){//分别维护左右边界if(q[i].x-q[i].r<=0) Left[Find_F(i)]=1;if(q[i].x+q[i].r>=w) Right[Find_F(i)]=1;}int ans=0;for(int i=0;i<n;i++){//如果左右边界都被某个圆集合覆盖,则增加答案if(Find_F(i)==i&&Left[Find_F(i)]==1&&Right[Find_F(i)]==1) ans++;}printf("%d\n",ans);}
}

转载于:https://www.cnblogs.com/Chen-Jr/p/11007185.html

Gym 101915J(并查集)相关推荐

  1. Tree Restoration Gym - 101755F (并查集)

    There is a tree of n vertices. For each vertex a list of all its successors is known (not only direc ...

  2. gym:Problem A Artwork(并查集思维题)

    20162017-acmicpc-nordic-collegiate-programming-contest-ncpc-2016 Problem A Artwork 题目链接 http://codef ...

  3. Por Costel and the Match Gym - 100923H(经典种类并查集)

    Por Costel and the Match Gym - 100923H 题目链接:https://vjudge.net/problem/Gym-100923H 题目: Oberyn Martel ...

  4. 【BFS】【并查集】【Tarjan】【LCA】Gym - 101173H - Hangar Hurdles

    给你一张地图,给你q次询问,每次问你从A点到B点,最大能移动多大的箱子. 把每个点所能容纳的最大箱子求出来(BFS,八连通,一开始将所有边界点和障碍点入队).然后从大到小排序.然后用并查集将相邻(四联 ...

  5. Friendly Group Gym - 102769F 2020(并查集)ccpc秦皇岛分站赛

    题意: n个学生要组成一个小组参加会议(可以不参加), 1.对于每两个朋友(x ,y),如果他们俩都参加会议,该小组的友好价值将会增加 1:如果其中只有一位参加会议,则该组的友好价值将降低 1. 3. ...

  6. Gym - 101194G Pandaria (并查集+倍增+线段树合并)

    题意: 给定一个无向图.每个点有一种颜色.现在给定q个询问,每次询问x和w,求所有能通过边权值不超过w的边走到x的点的集合中,哪一种颜色的点出现的次数最多.次数相同时输出编号最小的那个颜色.强制在线. ...

  7. Codeforces Gym 101194G Pandaria (2016 ACM-ICPC EC-Final G题, 并查集 + 线段树合并)

    题目链接  2016 ACM-ICPC EC-Final Problem G 题意  给定一个无向图.每个点有一种颜色. 现在给定$q$个询问,每次询问$x$和$w$,求所有能通过边权值不超过$w$的 ...

  8. 并查集c++代码_[Leetcode 每日精选](本周主题-并查集) 547. 朋友圈

    题目难度: 中等 原题链接 今天继续来做并查集的问题, 这道题仍然比较基础, 而且也是个比较接近现实的问题了. 大家在我的公众号"每日精选算法题"中的聊天框中回复 并查集 就能看到 ...

  9. HDU1811 Rank of Tetris 拓扑排序+并查集 OR 差分约束最短路+并查集

    题目链接 题意:就是给你一堆关系,看能不能排出个确定的顺序 做法: 1. 拓扑排序+并查集 应该很容易想到的一种思路,大于小于建立单向边.对于相等的呢,就把他们缩成一个点.就用并查集缩成一个点就行了 ...

最新文章

  1. 如何使用命令行清除NuGet包缓存?
  2. 有趣的库:pipe(类似linux | 管道)库
  3. android怎么关应用程序,如何关闭Android应用程序?
  4. leetcde-27-移除元素
  5. 将您重定向的次数过多什么意思_忙忙碌碌将爱麻木是什么歌-所以会忙忙碌碌将爱麻木歌曲意思、出处、含义介绍...
  6. vSphereClient向ESXi主机分配许可证
  7. Android接收系统广播
  8. day07_mysql基本操作
  9. matplotlib学习
  10. java课程设计仓库管理系统_Java课程设计-仓库管理系统
  11. 2011年IT行业薪资调查报告
  12. 高通MSM8998芯片参考资料免费下载
  13. c语言实现循环结构的语句有哪些?它们的区别是什么?,2011年04月份计算机软件基础(一)复习资料二...
  14. 十大不良习惯让你衰老速度加快
  15. GDR(Gradual Decoding Refresh, GradualDecoder Refresh)
  16. 将开发板的usb配置为ncm网口(qnx系统)
  17. 【数据分析】黑色星期五(代码2)销售额分析1、2
  18. 软件测试分类-按照开发阶段划分
  19. 大数据系列 -- 数据埋点
  20. 香农费诺编码 c语言实现,对于香农编码、费诺编码和哈夫曼编码,编码方法惟一的是()。...

热门文章

  1. SVN代码回滚命令之---merge的使用
  2. HDU 1018 Big Number
  3. 华为C8825D刷机失败解决方法
  4. 进程外Session保存和全局文件错误捕获
  5. JDK和JRE它们之间的关系及区别
  6. 入侵检测系统的性能的辨别(3)
  7. 阿里淘系50+工程师整理的 CV 学习资源清单(2021最新版)
  8. ICCV 2021 第二届无人车视觉(AVVision)研讨会征稿
  9. 官宣:OpenMMLab 重磅升级—百花齐放春满园
  10. ICCV 2019 | 厦大提出快速NAS检索方法,四小时搜索NN结构