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, ...
随机推荐
- ASP.NET数据绑定控件
数据绑定控件简介 数据绑定分为:数据源 和 数据绑定控件 两部分,数据绑定控件通过数据源来获得数据,通过数据源来隔离数据提供者和数据使用者,数据源有:SqlDataSource,AccessDataS ...
- Scala - error: not found: value SortedMap
先 IMPORT!!!! scala> import scala.collection._import scala.collection._ scala> SortedMap(" ...
- mysql 5.7安装脚本
[root@HE2 ~]# cat mysql_auto_install.sh ###### 二进制自动安装数据库脚本root密码MANAGER将脚本和安装包放在/root目录即可########## ...
- thinkphp ajax检测是否数据可用
模板文件:如 index.html 模板文件如:(index.html) <form><input type="text" name="username ...
- 求和函数 sum详解
sum()的参数是一个list: >>> sum([1,2,3]) 6 >>> sum(range(1,3)) 3 还有一个比较有意思的用法 a = range(1 ...
- 在Yii2.0中实现计划任务(cron)
以下由我们在信易网络公司开发项目的时候终结出的一些经验 Create console application 创建命令行应用 In advance template there is already ...
- php 需熟练掌握的几个函数
class Test { } $obj = new Test; 一.__construct() {} 构造函数 二.__destroy() {} 析构函数 三.__get() {} 试图读取一个并不 ...
- C语言和C++中动态申请内存
在C语言和C++的动态内存的使用方法是不同的,在C语言中要使用动态内存要包含一个头文件即 #include<malloc.h> 或者是#include<stdlib.h> ...
- 跨域Ajax请求WebService方法
一.允许跨域Ajax请求,更改如下配置: 在要调用的WebService上面添加特性标签: 二.以如下返回用户信息的WebService方法为例 三.在另一个网站上通过Ajax访问webService ...
- 开发C# .net时使用的数据库操作类SqlHelp.cs
练习开发WPF程序的时候,是这样写的,虽然很简单,相必很多新手会用到,所以拿来共享一下, using System; using System.Collections.Generic; using S ...