Description

了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤[1..10^9];Xi,Yi∈整数.当满足下列两个条件之一,两只奶牛i和j是属于同一个群的:
  1.两只奶牛的曼哈顿距离不超过C(1≤C≤10^9),即lXi - xil+IYi - Yil≤C.
  2.两只奶牛有共同的邻居.即,存在一只奶牛k,使i与k,j与k均同属一个群.
  给出奶牛们的位置,请计算草原上有多少个牛群,以及最大的牛群里有多少奶牛

Input

第1行输入N和C,之后N行每行输入一只奶牛的坐标.

Output

 仅一行,先输出牛群数,再输出最大牛群里的牛数,用空格隔开.

Sample Input

4 2
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

2 3

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.

—————————————————————————————
这道题首先我们进行一个转化 就是 将每个点变成(sx=(x+y), sy=( x-y))
这样两点的曼哈顿距离就是 max( |sx1 – sx2|, |sy1 – sy2|)=max( x1-x2+y1-y2 ,  x1-x2-y1+y2 )
这个可以分类证明 分四类
1 x1>x2 &&y1>y2 那么 sx1-sx2 就是曼哈顿距离的正常表示方法 且sx1-sx2>sy1-sy2 
2 x1<x2&&y1<y2 因为是绝对值 所以其实形式是一样的 (同上)
3 x1>x2&&y1<y2  那么 |x1-x2|+|y1-y2| = x1-x2+y2-y1 就是 sy1-sy2 也易得 sy1-sy2>sx1-sx2
3 x1>x2&&y2>y1 一样是绝对值 倒一下就好了(同上)
这样之后我们把点按 sx 排序 维护一个队列 对头的坐标和当前点的坐标的sx的差 不能超过 c
因为超过 c 他必然不可能和当前点曼哈顿距离小于c
所以如果超过我们要删除(顺便在平衡树把这个点删了)
然后我们维护一个sy的平衡树 求一下当前点sy在平衡树里面的前驱后继 看一下能不能合并
如果可以就合并在一个并查集  最后扫一波就可以得到答案了
至于为什么可以这么合并 因为就算别的点在前驱的下面或者后继的下面并且他们可以和当前点合并
那么他们一定在之前和前驱后继或者是再前再后合并 所以这样的贪心是可行的
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
const int M=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,c;
int f[M],h[M],ans,mx;
int find(int x){while(f[x]!=x) x=f[x]=f[f[x]]; return x;}
struct node{
int x,y,id;
bool operator <(const node& k)const{return x<k.x;}
}e[M];
struct pos{
int y,id;
bool operator <(const pos& k)const{return y!=k.y?y<k.y:id<k.id;}
};
std::queue<int>q;
std::multiset<pos>tr;
std::multiset<pos>::iterator it;
int main(){
int x,y;
n=read(); c=read();
for(int i=;i<=n;i++){
x=read(); y=read();
f[i]=i; e[i].x=x+y,e[i].y=x-y; e[i].id=i;
}
sort(e+,e++n);
for(int i=;i<=n;i++){
while(!q.empty()&&e[i].x-e[q.front()].x>c){
int now=q.front(); q.pop();
tr.erase(tr.find((pos){e[now].y,e[now].id}));
}
q.push(i);
it=tr.insert((pos){e[i].y,e[i].id});
if(it!=tr.begin()){
--it;
if(e[i].y-(it->y)<=c){
int p=find(e[i].id),q=find(it->id);
f[q]=p;
}
++it;
}
++it;
if(it!=tr.end()){
if(it->y-e[i].y<=c){
int p=find(e[i].id),q=find(it->id);
f[q]=p;
}
}
}
for(int i=;i<=n;i++){
int x=find(e[i].id);
if(!h[x]) ans++;
h[x]++; mx=max(mx,h[x]);
}printf("%d %d\n",ans,mx);
return ;
}

bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set的更多相关文章

  1. BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    题目 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Time Limit: 5 Sec  Memory Limit: 64 MB Description ...

  2. bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...

  3. BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居:队列 + multiset + 并查集【曼哈顿距离变形】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1604 题意: 平面直角坐标系中,有n个点(n <= 100000,坐标范围10^9) ...

  4. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 曼哈顿生成树

    大致题意:统计平面上由曼哈顿距离小于等于c的点对组成联通块的个数. 曼哈顿生成树的模板题.有关讲解:http://blog.csdn.net/acm_cxlove/article/details/88 ...

  5. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居【切比雪夫距离+并查集+multiset】

    参考:http://hzwer.com/4361.html 坐标开long long,inf开大点 先曼哈顿转切比雪夫(x+y,x-y),距离就变成了max(x',y'): 先按x排序,维护两个指针, ...

  6. BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap

    题意:链接 方法: Treap 解析: 前几道资格赛的题水的不行,这道Gold的题就够分量辣. 首先这个曼哈顿距离啥的肯定能做文章,怎么转化是个问题,自己玩了一会没玩出来,就查了查曼哈顿距离的转化,发 ...

  7. 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1604 这题太神了... 简直就是 神思想+神做法+神stl.. 被stl整的我想cry...首先,, ...

  8. 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [算法]并查集+平衡树+数学+扫描线 [题解] 经典曼哈顿距离转切比雪夫距离. 曼哈顿距离:S=|x1-x2|+|y1-y2|<=c 即:max(x1-x2+y1-y2,x1-x2-y1+y2, ...

  9. bzoj 1623: [Usaco2008 Open]Cow Cars 奶牛飞车【排序+贪心】

    从小到大排个序,然后能选就选 #include<iostream> #include<cstdio> #include<algorithm> using names ...

随机推荐

  1. Java byte 位移操作 注意事项

    转自:http://blog.163.com/pilgrim_yang/blog/static/55631481201111542151582/ Java对byte 的 + - * / >> ...

  2. JavaScript函数constructor的作用,意义

    前几天写了一片 如何用正确的姿势编写jQuery插件 有朋友拍砖,指正.再此谢谢! 讨论:指定函数的constructor作用到底是什么? 我们一般写jQuery插件的时候是这样的: //构造函数 f ...

  3. 存一些有用的CSS

    reset ;} table{} fieldset,img{} address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;fon ...

  4. spring boot 连接mysql 错误The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one

    1.spring boot 整合mybatis 连接mysql时错误 The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or repr ...

  5. C#3DES加密了解一下

    最近一个项目中,因为服务端是用的java开发的,客户端是用的C#,由于通信部分采用到了3DES加密,所以做个记录,以备以后需要的时候直接用. 这是对方(java)的加密算法,和网上流传的代码也差不多( ...

  6. 【app.js】配置及App函数说明

    app.js中的App函数用来注册一个小程序或设置全局变量. App函数: 语法:App(Object)     参数: Object json对象     说明: App函数必须在app.js中调用 ...

  7. 教你如何用Docker快速搭建深度学习环境

    本教程搭建集 Tensorflow.Keras.Coffe.PyTorch 等深度学习框架于一身的环境,及jupyter. 本教程使用nvidia-docker启动实例,通过本教程可以从一个全新的Ub ...

  8. JavaScript - arguments object

    The arguments object is an Array-like object corresponding to the arguments passed to a function. fu ...

  9. java--List转换成json格式

    方法一 首先导入jar包,json-rpc-1.0.jar public class List2Json { public static JSONArray ProLogList2Json(List& ...

  10. HashSet如何判定两个元素相同

    在介绍java的集合时,我们提到,set是一个"罐子".我们可以向其中放入各式各样的元素,这些元素没有顺序,但不能相同.其中,HashSet是最常用的一个实现类. 首先,我们看下H ...