City Park

题目连接:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122283#problem/F

Description

Porto is blessed with a beautiful city park. The park, in

the western section of the city, borders the Atlantic Ocean.

It has great lawns, small forests, plenty of flowerbeds, a

variety of ponds, and, in all, lots of points of interest.

Porto families love the park and flock to in on weekends

and holidays.

With such multitudes, it is hard work to keep the

lawns in good shape. In order to control the movements

of the crowd, the engineers of the municipality designed

a system of paths connecting points of interest. These

paths are built with large rectangular shale stones from the nearby Milh´aria quarry. Using sophisticated

location systems, the engineers were able to lay the stones perfectly aligned with the north-south

direction (and hence also with the east-west direction). Stones linking one from one point of interest to

another touch each other, forming a contiguous stoned surface, and do not touch any stones belonging

to any other stoned surface.

The “defend our park” movement wants to stage a demonstration in the park to publicise their

cause. Since they do not want to harm the lawns, they must stage the demonstration in one of those

stoned surfaces. In order to summon as many supporters as possible, but not too many, they need to

find out the area of the stoned surface with largest area.

Given the locations and dimensions of stones in the park, compute the area of the stoned surface

with the largest area.

Input

The input file contains several test cases, each of them as described below.

The first line contains one positive integer, N, representing the number of rectangular stones. N

lines follow, each one describing the location and dimensions of a stone, by four integers, X, Y , W,

H, where (X, Y ) are the coordinates of the location of the lower left corner the stone, W is its length

along the x-axis, and H is its length along the y-axis.

Constraints:

0 < N ≤ 50 000 Number of stones.

0 < W ≤ 500, 0 < H ≤ 500 Dimensions of stones.

It is guaranteed that, for the given inputs, the coordinates of the stone corners can be handled using

normal 32-bit signed integers, as well as the total area of any stoned surface. For every pair of distinct

stones, the area of the intersection of the two rectangles that represent them in the park is zero (i.e.,

there are no overlaps).

Output

For each test case, write to the output a single line with an integer: the area of the stoned surface with

largest area.

Sample Output Explanation

The following figure represents the configuration of stones described in the sample input.

There are 4 stoned surfaces: one made up by stones 3 and 4, on the left, with area 16; another,

made up by stones 7 and 1, with area 20; a third one, below the previous, made up by stones 0, 2 and

6, with area 15; and the one on the right, made up by stone 5 only, with area 16. The largest area is

20.

Sample Input

8

14 1 2 2

16 9 1 5

11 3 5 2

3 4 2 5

5 9 3 2

21 3 2 8

13 2 1 1

13 8 3 5

Sample Output

20

Hint

题意

在平面上给你n个矩形,你需要找到最大的连通块面积是多少,两个矩形只要点相接触,就连通

保证矩形之间不会重叠

题解:

因为两个点相邻,就连通嘛,那就按照x轴排个序,然后并茶几合并一下。

y轴排个序,并茶几合并一下就好了。

代码里面的random是拿来卖萌的-.-

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2100050;
int fa[maxn],n;
long long val[maxn];
long long val2[maxn];
struct node{
int x0,y0,x1,y1,x2,y2,x3,y3,x4,y4;
int id;
node(int X0,int Y0,int X1,int Y1,int X2,int Y2,int X3,int Y3,int X4,int Y4,int ID):x0(X0),y0(Y0),x1(X1),y1(Y1),x2(X2),y2(Y2),x3(X3),y3(Y3),x4(X4),y4(Y4),id(ID){};
};
vector<node> op;
bool cmp1(node a,node b){
if(a.x0==b.x0)return a.y0<b.y0;
return a.x0<b.x0;
}
bool cmp2(node a,node b){
if(a.y0==b.y0)return a.x0<b.x0;
return a.y0<b.y0;
}
bool cmp3(node a,node b){
if(a.y0==b.y0)return a.x0>b.x0;
return a.y0<b.y0;
}
bool cmp4(node a,node b){
if(a.x0==b.x0)return a.y0>b.y0;
return a.x0<b.x0;
}
int fi(int x){
if(fa[x]==x)return x;
fa[x]=fi(fa[x]);
return fa[x];
}
void uni(int x,int y){
int p=fi(x),q=fi(y);
if(p==q)return;
fa[p]=q;
}
bool check(node aa,node bb){
if(aa.x0==bb.x0&&aa.y0==bb.y0)return true;
if(aa.x1==bb.x1){
if(aa.y1<=bb.y1&&aa.y3>=bb.y1)return true;
if(aa.y1<=bb.y3&&aa.y3>=bb.y3)return true;
}
if(aa.x2==bb.x2){
if(aa.y1<=bb.y1&&aa.y3>=bb.y1)return true;
if(aa.y1<=bb.y3&&aa.y3>=bb.y3)return true;
}
if(aa.x1==bb.x2){
if(aa.y1<=bb.y1&&aa.y3>=bb.y1)return true;
if(aa.y1<=bb.y3&&aa.y3>=bb.y3)return true;
}
if(aa.x2==bb.x1){
if(aa.y1<=bb.y1&&aa.y3>=bb.y1)return true;
if(aa.y1<=bb.y3&&aa.y3>=bb.y3)return true;
}
if(aa.y1==bb.y1){
if(aa.x1<=bb.x1&&aa.x2>=bb.x1)return true;
if(aa.x1<=bb.x2&&aa.x2>=bb.x2)return true;
}
if(aa.y1==bb.y3){
if(aa.x1<=bb.x1&&aa.x2>=bb.x1)return true;
if(aa.x1<=bb.x2&&aa.x2>=bb.x2)return true;
}
if(aa.y3==bb.y3){
if(aa.x1<=bb.x1&&aa.x2>=bb.x1)return true;
if(aa.x1<=bb.x2&&aa.x2>=bb.x2)return true;
}
if(aa.y3==bb.y1){
if(aa.x1<=bb.x1&&aa.x2>=bb.x1)return true;
if(aa.x1<=bb.x2&&aa.x2>=bb.x2)return true;
}
return false;
}
void work(){
srand(time(NULL));
op.clear();
memset(val,0,sizeof(val));
memset(val2,0,sizeof(val2));
for(int i=0;i<maxn;i++)fa[i]=i;
for(int i=0;i<maxn;i++)val[i]=0;
for(int i=1;i<=n;i++){
int xx,yy,ll,rr;
scanf("%d%d%d%d",&xx,&yy,&ll,&rr);
op.push_back(node(xx,yy,xx,yy,xx+ll,yy,xx+ll,yy+rr,xx,yy+rr,i));
op.push_back(node(xx+ll,yy,xx,yy,xx+ll,yy,xx+ll,yy+rr,xx,yy+rr,i));
op.push_back(node(xx,yy+rr,xx,yy,xx+ll,yy,xx+ll,yy+rr,xx,yy+rr,i));
op.push_back(node(xx+ll,yy+rr,xx,yy,xx+ll,yy,xx+ll,yy+rr,xx,yy+rr,i));
val[i]=1ll*ll*rr;
}
long long ans = 0; sort(op.begin(),op.end(),cmp1);
for(int i=0;i<op.size()-1;i++){
if(check(op[i],op[i+1]))
uni(op[i].id,op[i+1].id);
}
sort(op.begin(),op.end(),cmp2);
for(int i=0;i<op.size()-1;i++){
if(check(op[i],op[i+1]))
uni(op[i].id,op[i+1].id);
}
sort(op.begin(),op.end(),cmp3);
for(int i=0;i<op.size()-1;i++){
if(check(op[i],op[i+1]))
uni(op[i].id,op[i+1].id);
}
sort(op.begin(),op.end(),cmp4);
for(int i=0;i<op.size()-1;i++){
if(check(op[i],op[i+1]))
uni(op[i].id,op[i+1].id);
}
random_shuffle(op.begin(),op.end());
for(int i=0;i<op.size()-1;i++){
if(check(op[i],op[i+1]))
uni(op[i].id,op[i+1].id);
}
for(int i=1;i<=n;i++)
{
fa[i]=fi(i);
val2[fa[i]]+=val[i];
ans=max(val2[fa[i]],ans);
}
cout<<ans<<endl;
}
int main(){
while(scanf("%d",&n)!=EOF){
work();
}
}

UVALive 6889 City Park 并查集的更多相关文章

  1. 并查集 - UVALive 6889 City Park

    City Park Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=129725 Mean: 在 ...

  2. HDU - 4496 City 逆向并查集

    思路:逆向并查集,逆向加入每一条边即可.在获取联通块数量的时候,直接判断新加入的边是否合并了两个集合,如果合并了说明联通块会减少一个,否则不变. AC代码 #include <cstdio> ...

  3. UVALive 4487 Exclusive-OR 加权并查集神题

    已知有 x[0-(n-1)],但是不知道具体的值,题目给定的信息 只有 I P V,说明 Xp=V,或者 I P Q V,说明 Xp ^ Xq=v,然后要求回答每个询问,询问的是 某任意的序列值 Xp ...

  4. UVALive - 3644 X-Plosives (并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  5. UVALive - 3027 Corporative Network (并查集)

    这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...

  6. UVALive 6910 Cutting Tree 并查集

    Cutting Tree 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

  7. UVALive 6906 Cluster Analysis 并查集

    Cluster Analysis 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemi ...

  8. UVALive 6187 Never Wait for Weights 带权并查集

    题意:每次给出每两个数之间的大小差值.在给出关系的过程中插入询问:数a和数b的差值,若不能确定,输出UNKNOWN 解法:相对大小关系的处理:并查集 1.给出两点的相对大小关系后,找到两个点的根节点, ...

  9. UVALive 7456 Least Crucial Node (并查集)

    Least Crucial Node 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/C Description http://7 ...

随机推荐

  1. WebViewJavascriptBridge测试示例

    android或ios:app与html5通信解决方案 下面只是前端示例代码,后端代码请参考: git https://github.com/marcuswestin/WebViewJavascrip ...

  2. Ubuntu 14.04 + gnome session back (metacity) 任务栏右上角图标消失问题解决

    没错, 就是说右上角的所有图标 (时间啊, 系统啊所有的)都消失了. 通过下列命令可以恢复 dconf reset -f /org/gnome/gnome-panel/ 参考这篇帖子: Upgrade ...

  3. tensorboard遇到的坑

    <ul><li>No graph definition files were found.</li></ul> <p>启动命令 tensor ...

  4. Linux驱动技术(五) _设备阻塞/非阻塞读写【转】

    转自:http://www.cnblogs.com/xiaojiang1025/p/6377925.html 等待队列是内核中实现进程调度的一个十分重要的数据结构,其任务是维护一个链表,链表中每一个节 ...

  5. zabbix 3.2.2 server端(源码包)安装部署 (一)【转】

    环境准备: 操作系统 CentOS 6.8 2.6.32-642.11.1.el6.x86_64 zabbix server 172.16.10.150 zabbix agent 172.16.10. ...

  6. 一个无锁消息队列引发的血案(四)——月:RingQueue(上) 自旋锁

    目录 (一)起因 (二)混合自旋锁 (三)q3.h 与 RingBuffer (四)RingQueue(上) 自旋锁 (五)RingQueue(中) 休眠的艺术 (六)RingQueue(中) 休眠的 ...

  7. 乐视max2 在开发中无法打印某些logcat 解决方案

    乐视屏蔽了打印log.d等类型logcat.解决方案:拨号键盘 *#*#76937#*#* 出现页面后选最下面那个选项就有了.

  8. printf 字符串格式

    摘自:http://www.cppblog.com/API/archive/2013/07/18/201923.html 首先 long long是C99标准新规定的.不少编译器还不支持,Micros ...

  9. [更新]一份包含: 采用RSA JWT(Json Web Token, RSA加密)的OAUTH2.0,HTTP BASIC,本地数据库验证,Windows域验证,单点登录的Spring Security配置文件

    没有任何注释,表怪我(¬_¬) 更新: 2016.05.29: 将AuthorizationServer和ResourceServer分开配置 2016.05.29: Token获取采用Http Ba ...

  10. vue1.0

    vue1.0学习总结   前言 使用vue已经有三.四个月了,但是只是学着使用了一些基本方法.因为现在的前端框架越来越多(Angular,React...),但是我相信万变不离其宗,很多用法框架之间还 ...