BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
题目
1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
Time Limit: 5 Sec Memory Limit: 64 MB
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.
题解
这题很明显就是再考并查集!【真的吗,你看下数据范围!】关于曼哈顿距离的技巧,将每个点变成(x+y,x-y)这样两个点之间的曼哈顿距离就是|x1-x2|+|y1-y2|,这样就可以维护一个平衡树,根据前驱和后继上面的节点来维护并查集求得答案了!
代码
/*Author:WNJXYK*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
using namespace std; #define LL long long
#define Inf 2147483647
#define InfL 10000000000LL inline void swap(int &x,int &y){int tmp=x;x=y;y=tmp;}
inline void swap(LL &x,LL &y){LL tmp=x;x=y;y=tmp;}
inline int remin(int a,int b){if (a<b) return a;return b;}
inline int remax(int a,int b){if (a>b) return a;return b;}
inline LL remin(LL a,LL b){if (a<b) return a;return b;}
inline LL remax(LL a,LL b){if (a>b) return a;return b;} inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,c,ans,mx;
int fa[100005],tot[100005];
struct data{LL x,y;int id;}a[100005];
multiset <data> b;
set <data>::iterator it;
inline bool operator<(data a,data b){
return a.y<b.y;
}
inline bool cmpx(data a,data b){
return a.x<b.x;
}
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
inline void un(int x,int y){
int p=find(x),q=find(y);
if(p!=q){
fa[p]=q;
ans--;
}
}
void solve(){
b.insert((data){0,InfL,0});b.insert((data){0,-InfL,0});
int now=1;b.insert(a[1]);
for(int i=2;i<=n;i++){
while(a[i].x-a[now].x>c){
b.erase(b.find(a[now]));
now++;
}
it=b.lower_bound(a[i]);
data r=*it,l=*--it;
if(a[i].y-l.y<=c)
un(a[i].id,l.id);
if(r.y-a[i].y<=c)
un(a[i].id,r.id);
b.insert(a[i]);
}
}
int main(){
n=read();c=read();ans=n;
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=1;i<=n;i++){
int t1=read(),t2=read();
a[i].x=t1+t2,a[i].y=t1-t2;a[i].id=i;
}
sort(a+1,a+n+1,cmpx);
solve();
for(int i=1;i<=n;i++)
tot[find(i)]++;
for(int i=1;i<=n;i++)
mx=max(mx,tot[i]);
printf("%d %d\n",ans,mx);
return 0;
}
BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居的更多相关文章
- bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...
- 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, ...
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...
随机推荐
- openGl学习之加入颜色
OpenGL 支持两种颜色模式:一种是 RGBA模式.一种是 颜色索引模式. 不管哪种颜色模式.计算机都必须为每个像素保存一些数据,即通过每个像素的颜色,来改变总体图形的颜色.不同的是. RGBA 模 ...
- ZOJ3689 Digging(01背包)
#include <iostream> #include <cstdio> #include<cmath> #include<algorithm> #i ...
- Node.js、Ionic、Cordova、AngualrJS安装
1.安装node.js: 从node.js官网下载node.js安装包,node.js下载地址:https://nodejs.org/en/download/,选择对应系统的安装下载后进行安装.(注: ...
- HDU2602-Bone Collector
描述: Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man l ...
- 使用iscroll4可能会遇到的问题(转:记录)
1.在iscroll4的滚动容器范围内,点击input框.select等表单元素时没有响应这个问题原因在于iscroll需要一直监听用户的touch操作,以便灵敏的做出对应效果,所以它把其余的默认事件 ...
- [LeetCode]题解(python):007-Reverse Integer
题目来源: https://leetcode.com/problems/reverse-integer/ 题意分析: 这道题目很简单,就是将一个数反转,123变321,-123变321. 题目思路: ...
- 一张图解释---Java多态
1.向上转型:编译器自动进行,不需要声明 Snowboard s = new Snowboard (); Object o = s; (相当于指向Snowboard的内部Object实例,所有类都继承 ...
- 什么是core dump?(转)
什么是Core Dump? 今天调试一个程序, 用到了core dump, 于是写出来, 记于此.什么是Core Dump?Core的意思是内存, Dump的意思是扔出来, 堆出来.开 发和使用Uni ...
- setTimeout 虚假的“异步”
看这篇http://www.laruence.com/2009/09/23/1089.html 所以实际上 setTimeout更像是任务按照队列执行 经过setTimeout设置后任务放在了队尾 ...
- 宣布发布 Windows Azure ExpressRoute,宣告与 Level 3 建立全新的合作伙伴关系并推出关于其他 Azure 服务令人振奋的更新
在我们与世界各地的客户和合作伙伴交谈时,总会听到他们说,希望找到一个提供商帮助他们最大限度地发挥内部部署投资的作用并且能够利用云的灵活性.这是我们构建混合云策略和云操作系统愿景的基本原则.本着我 ...