bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)
Description
Input
第1行输入N和C,之后N行每行输入一只奶牛的坐标.
Output
仅一行,先输出牛群数,再输出最大牛群里的牛数,用空格隔开.
Sample Input
1 1
3 3
2 2
10 10
* Line 1: A single line with a two space-separated integers: the
number of cow neighborhoods and the size of the largest cow
neighborhood.
Sample Output
OUTPUT DETAILS:
There are 2 neighborhoods, one formed by the first three cows and
the other being the last cow. The largest neighborhood therefore
has size 3.
HINT
Source
【思路】
Set+并查集
曼哈顿距离分为以下情况
X-x+Y-y -> (X+Y)-(x+y)
X-x+y-Y -> (X-Y)-(x-y)
x-X+Y-y -> -( (X-Y)-(x-y) )
x-X+y-Y -> -( (X+Y)-(x+y) )
这四种情况,我们可以发现,答案就是
max( |(X+Y)-(x+y)|, |(X-Y)-(x-y)| )
于是把每个点变为(x+y,x-y) 并将x从小到大排序
维护一个队列使得front~i区间x坐标的差均小于c
用set维护队列元素中的y,如果i在set中的前驱或后继与i的y之差小于c则合并集合。
【代码】
#include<set>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long LL;
const int N = 1e5+;
const int INF = 1e9+1e9+;
struct Node{
int x,y;
bool operator < (const Node& rhs) const{
return x<rhs.x;
}
}a[N];
int n,c,p[N],sum[N],ans,mx;
multiset<Node> S;
multiset<Node> ::iterator it; int ifind(int u) {
return u==p[u]? u:p[u]=ifind(p[u]);
}
void unit(int x,int y) {
x=ifind(x),y=ifind(y); if(x!=y) p[x]=y;
} void read(int& x) {
char c=getchar(); int f=; x=;
while(!isdigit(c)){if(c=='-')f=-; c=getchar();}
while(isdigit(c)) x=x*+c-'',c=getchar();
x*=f;
}
int main() {
read(n),read(c);
for(int i=;i<=n;i++) p[i]=i;
int x,y;
for(int i=;i<=n;i++) {
read(x),read(y);
a[i].x=x+y,a[i].y=x-y;
}
sort(a+,a+n+);
S.insert((Node){INF,}),S.insert((Node){-INF,});
int front=;
for(int i=;i<=n;i++) {
while(a[i].x-a[front].x>c) {
S.erase(S.lower_bound((Node){a[front].y,front}));
++front;
}
it=S.lower_bound((Node){a[i].y,i});
Node r=*it,l=*(--it);
if((LL)r.x-(LL)a[i].y<=c) unit(r.y,i);
if((LL)a[i].y-(LL)l.x<=c) unit(l.y,i);
S.insert((Node){a[i].y,i});
}
for(int i=;i<=n;i++) {
sum[ifind(i)]++;
if(sum[ifind(i)]==) ans++;
}
for(int i=;i<=n;i++) mx=max(mx,sum[i]);
printf("%d %d",ans,mx);
return ;
}
bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)的更多相关文章
- BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
题目 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Time Limit: 5 Sec Memory Limit: 64 MB Description ...
- 【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000) ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...
- BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居:队列 + multiset + 并查集【曼哈顿距离变形】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1604 题意: 平面直角坐标系中,有n个点(n <= 100000,坐标范围10^9) ...
- 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 奶牛的邻居(set+并查集+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1604 这题太神了... 简直就是 神思想+神做法+神stl.. 被stl整的我想cry...首先,, ...
- 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[算法]并查集+平衡树+数学+扫描线 [题解] 经典曼哈顿距离转切比雪夫距离. 曼哈顿距离:S=|x1-x2|+|y1-y2|<=c 即:max(x1-x2+y1-y2,x1-x2-y1+y2, ...
随机推荐
- linux删除、读取文件原理
linux删除文件原理 LINUX的文件名是存在父目录的block里面,并指向这个文件额inode节点,这个文件的inode节点再标记指向存放这个文件的block的数据块.我们删除一个文件,实际上并不 ...
- thinkphp 缓存写入失败,网站报错
周末,正在家里休息,同事突然call 我,说网站打不开了,网站一直很正常的,突然成这样,肯定某个地方出问题了, 原来是网站所在的硬盘分区,没空间了,被mysql的日志占满了!!! 哎,好好的周末,在公 ...
- wamp5 忘记mysql root密码 重置方法
wamp5比较恶心,忘记mysql root密码了,重新安装都没用,网上找了个文章可以修改root的密码. http://www.kuqin.com/database/20080306/4249.ht ...
- 怎么样调试正在运行的exe?
最近在调虚幻的编辑器的时候遇到了一个问题. 调试模式运行UE4Editor.exe 实际上只是一个带参的命令行. 打开后,它又通过这个参数生成了一份详细配置,重新调用了自己.如图 这就悲剧了,断点都没 ...
- maya2105 - windows8 - numpy/scipy
To compile numpy, create a site.cfg file in numpy's source directory with the following or similar c ...
- python学习--string
1\string are immutable, which means you can't change an existing string. >>>greeting = 'Hel ...
- 缓存 Cache
Controllers层 public class HomeController : Controller { // // GET: /Home/ // ...
- 有两个数组a,b,大小都为n;通过交换a,b中的元素,使sum(a)-sum(b)最小。
今天在浏览网页的时候,发现了一个叫做 华为面试题(8分钟写出代码) 的链接,不确定真实性,纯属好奇,就点进去看看 这个可能是很老的题目吧,因为我看到这题目时,底下有好多评论了.提到XX排序,内存占用 ...
- 2015年9月29日 sql 触发器
触发器(trigger):当有关联操作的时候使用(级联操作),属于ddl关键字. eg:下订单时,创建中的商品数量要减少:退票时,总的票数要增加. 在订单上建立触发器 ...
- 《无懈可击的Web设计》_灵活的文字
常见的方法 body{ font-size:12px; } 使用像素单位的优点: 不管使用哪种浏览器或者设备来查看,文字看起来都大小一致. 为什么这么做不是无懈可击的? 以像素为单位设定文字大小之后, ...