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, ...
随机推荐
- QT 自定义assert
预览 代码 #define assert_(expression,message) if (expression) \ { \ if (QMessageBox::Yes == QMessageBoxE ...
- STL中map与hash_map的比较
1. map : C++的STL中map是使用树来做查找算法; 时间复杂度:O(log2N) 2. hash_map : 使用hash表来排列配对,hash表是使用关键字来计算表位置; 时间复杂度:O ...
- 项目中logger、message错误信息的配置
申明:在一个项目中必不可少的是Logger和错误信息的配置,现在给出在我们常用的处理方法. —.创建一个ConfigUtils类和他对应的rah.properties文件和Test测试类 Config ...
- z-index使用以及失效的处理方法
1.一般z-index的使用是在有两个重叠的标签,在一定的情况下控制其中一个在另一个的上方出现. 2.z-index值越大就越是在上层.z-index:9999:z-index元素的position属 ...
- Cassandra1.2文档学习(1)——Cassandra基本说明
参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/a ...
- phpcms(1)phpcms V9 MVC模式 与 URL访问解析(转)
[1]URL访问解析 观察访问网页时的网址,可以得出模块访问方法,如下示例: http://www.abcd.com.cn/phpcms/index.php?m=content&c=index ...
- uploadify实现七牛云存储 显示上传进度+页面显示
准备: uploadify下载地址: http://www.uploadify.com/download/ 七牛 php-sdk开发指南: http://developer.qiniu.com/doc ...
- 粘滞位(sticky bit)
linux特殊权限:setUid, setGid, 粘着位(sticky) (1)目录的X权限(执行) 文件的可执行权限很简单,也就是可否执行它的意思,但目录的执行权限又代表什么意思呢? 当然不可能是 ...
- C++ 11 笔记 (六) : 随机数
以前生成一个随机数都是这样: srand(time(NULL)); rand(); 在C++11中,标准库中增加了随机数引擎 std::default_random_engine 这个好东西,然后我们 ...
- 打开网页自动弹出qq客户端
新建js后调用即可,打开网站自动弹出qq对话框,若qq为关闭状态则启动qq,之后弹出对话框. document.write("<iframe src='tencent://messag ...