BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居:队列 + multiset + 并查集【曼哈顿距离变形】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1604
题意:
平面直角坐标系中,有n个点(n <= 100000,坐标范围10^9)。
给定r,当两个点的曼哈顿距离<=r时,认为这两个点在同一个“群”中。
问你共有多少个群,以及点的数量最多的群有多少个点。
题解:
本题的核心在于:如何枚举一个点周围满足“曼哈顿距离<=r”的点。
由于 曼哈顿距离 = |x1 - x2| + |y1 - y2|。
x和y相互影响,不能单纯按x或y排序,枚举所有点总复杂度为O(N^2)。
所以要用到曼哈顿距离的另一种形式:
设X = x + y , Y = x - y
d(曼哈顿距离) = max(|X1-X2|, |Y1-Y2|)
将每个点的X = x + y,Y = x - y,这就将X与Y的关系分离开了。
将所有点按X排序。
当前考虑到点i。
用一个队列(X升序),保证队列中的所有X满足要求,否则不断删去队首。
用一个multiset(Y升序),找到i的前驱pre和后继suc,如果i与pre(或suc)的Y满足要求,则合并(并查集)。
最后统计一下每个群就好了。
AC Code:
// |x1-x2| + |y1-y2|
// | (x1+y1) - (x2+y2) |
// | (x1-y1) - (x2-y2) |
// X = x + y
// Y = x - y
// d = max(|X1-X2|, |Y1-Y2|) <= r
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#define MAX_N 100005
#define INF 100000000 using namespace std; struct Coor
{
int x;
int y;
int idx;
Coor(int _x,int _y,int _idx)
{
x=_x;
y=_y;
idx=_idx;
}
Coor(){}
friend bool operator < (const Coor &a,const Coor &b)
{
return a.y!=b.y?a.y<b.y:a.idx<b.idx;
}
}; int n,r;
int ans=;
int counter=;
int head=;
int par[MAX_N];
int cnt[MAX_N];
Coor c[MAX_N];
multiset<Coor> mst; inline bool cmp_x(const Coor &a,const Coor &b)
{
return a.x<b.x;
} void init_union_find()
{
for(int i=;i<n;i++)
{
par[i]=i;
}
} int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);
} void unite(int x,int y)
{
int px=find(x);
int py=find(y);
if(px==py) return;
par[px]=py;
} void read()
{
cin>>n>>r;
int a,b;
for(int i=;i<n;i++)
{
cin>>a>>b;
c[i].x=a+b;
c[i].y=a-b;
c[i].idx=i;
}
} void solve()
{
init_union_find();
sort(c,c+n,cmp_x);
mst.insert(Coor(,INF,));
mst.insert(Coor(,-INF,));
mst.insert(c[head]);
for(int i=;i<n;i++)
{
while(c[i].x-c[head].x>r)
{
mst.erase(c[head]);
head++;
}
multiset<Coor>::iterator it=mst.lower_bound(c[i]);
Coor suc=*it;
Coor pre=*--it;
if(c[i].y-pre.y<=r) unite(pre.idx,c[i].idx);
if(suc.y-c[i].y<=r) unite(suc.idx,c[i].idx);
mst.insert(c[i]);
}
memset(cnt,,sizeof(cnt));
for(int i=;i<n;i++)
{
int p=find(i);
if(cnt[p]==) counter++;
cnt[p]++;
ans=max(ans,cnt[p]);
}
} void print()
{
cout<<counter<<" "<<ans<<endl;
} int main()
{
read();
solve();
print();
}
BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居:队列 + multiset + 并查集【曼哈顿距离变形】的更多相关文章
- bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...
- 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1604 这题太神了... 简直就是 神思想+神做法+神stl.. 被stl整的我想cry...首先,, ...
- BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
题目 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Time Limit: 5 Sec Memory Limit: 64 MB Description ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...
- 【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 旋转坐标系+并查集+Treap/STL-set
题目描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤ ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 曼哈顿生成树
大致题意:统计平面上由曼哈顿距离小于等于c的点对组成联通块的个数. 曼哈顿生成树的模板题.有关讲解:http://blog.csdn.net/acm_cxlove/article/details/88 ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居【切比雪夫距离+并查集+multiset】
参考:http://hzwer.com/4361.html 坐标开long long,inf开大点 先曼哈顿转切比雪夫(x+y,x-y),距离就变成了max(x',y'): 先按x排序,维护两个指针, ...
- BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap
题意:链接 方法: Treap 解析: 前几道资格赛的题水的不行,这道Gold的题就够分量辣. 首先这个曼哈顿距离啥的肯定能做文章,怎么转化是个问题,自己玩了一会没玩出来,就查了查曼哈顿距离的转化,发 ...
- 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[算法]并查集+平衡树+数学+扫描线 [题解] 经典曼哈顿距离转切比雪夫距离. 曼哈顿距离:S=|x1-x2|+|y1-y2|<=c 即:max(x1-x2+y1-y2,x1-x2-y1+y2, ...
随机推荐
- Openstack nova代码部分凝视一
做个一个没怎么学过python的菜鸟.看源代码是最好的学习方式了,如今就从nova入手,主要凝视一下 nova/compute/api.py 中的 create_instance函数 def _cre ...
- 微信小程序 - 时间戳转时间
获取当前时间:十位unix时间戳 var timestamps = Math.round(new Date().getTime() / 1000).toString(); 时间戳转时间(官方自带) 使 ...
- 【SharePoint】SharePoint 2013 使用PreSaveAction自定义客户端验证
使用PreSaveAction函数实现客户端自定义验证. 例:[项目编号]为空时,必须填写[责任者]项.(其中[项目编号]为单行文本框,[责任者]为用户/组选择框.) function PreSave ...
- 在非主线程中更新UI
在非主线程中调用了showMessage方法,结果报错:Can't create handler inside thread that has not called Looper.prepare() ...
- java 中 instanceof 和 isInstance区别
两者的功能是等价的.区别: 1.instanceof 是一个操作符(类似new, ==等): 使用方法: if (ins instanceof String) { //logic } 2.isInst ...
- 精彩回顾 HUAWEI HiAI 亮相华为北研所
从普通照片变成艺术品,仅需3秒: 从随手拍下的讲解胶片到生成规整清晰的ppt,只要瞬间…… 5月25日在华为北京研究所举办的HUAWEI HiAI技术合作交流会上,伴随着一声声惊叹,数款接入HUA ...
- ArrayList中contains,remove方法返回为false的原因
这几天做一个项目时,遇到ArrayList.remove(Object)方法失败,而ArrayList"包含"删除的对象,这其中的"包含"不是完全包含,请看下面 ...
- 巧用Excel提高工作效率
程序员如何巧用Excel提高工作效率 主要讲解下Excel中VLOOKUP函数的使用,相比于上一篇中的内容,个人觉得这个相对高级一些. 1.使用背景 为什么会使用到这个函数呢,背景是这样的,有两个系统 ...
- PowerBuilder -- 指定重复的列不显示
- nstall-Package : 无法找到程序包“MySql.Data.Entity.EF6”
在vs2013中用MySQL+EF6时,会遇到版本的问题,解决方法一般如下: 1 Install-Package EntityFramework -Version 6.0.0然后Enter2 Inst ...