(最近p站上不去要死了)

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.

这道题调死我了。。。

其实主要是曼哈顿距离的运用。对于两个点(x1,y1)和(x2,y2),直接的曼哈顿距离是|x1-x2|+|y1+y2|。但这个绝对值太麻烦了,我们希望把它去掉。于是把点坐标转化一下:(A1,B1)和(A2,B2)(A1=x1+y1,B1=x1-y1,2同理)。于是曼哈顿距离就是max(A1-A2,B1-B2)。

所以就把每个奶牛按新的横坐标排序。把她们挨个挨个扔进平衡树(set)里面(当然要保证平衡树里面的奶牛新横坐标之差都小于等于c),然后查找新纵坐标的前驱后继判断差值是否满足题意。如果小于等于c,就用并查集合并起来。

#include<set>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N=100000+5;
const int oo=0x3f3f3f3f; int n,c,fa[N],siz[N];
bool vis[N];
struct node{
int x,y;
int num;
}a[N];
set<node> S; inline bool operator < (const node &a,const node &b){
if(a.y==b.y) return a.num<b.num;
return a.y<b.y;
} bool cp(const node &a,const node &b){
return a.x==b.x&&a.y==b.y&&a.num==b.num;
}
bool cmp(const node &a,const node &b){
return a.x<b.x;
}
int getfa(int x){
if(fa[x]==x) return x;
return fa[x]=getfa(fa[x]);
}
void merge(int x,int y){
int fx=getfa(x),fy=getfa(y);
if(fx==fy) return ;
fa[fx]=fy;
}
int main(){
scanf("%d%d",&n,&c);
int xx,yy;
for(int i=1;i<=n;i++){
scanf("%d%d",&xx,&yy);
a[i].x=xx+yy,a[i].y=xx-yy;
a[i].num=i;
fa[i]=i;
}
sort(a+1,a+n+1,cmp);
node inf,_inf;
inf.x=_inf.x=inf.num=_inf.num=0,inf.y=oo,_inf.y=-oo;
S.insert(inf);
S.insert(_inf);
S.insert(a[1]);
node pre,sub;
int tmp=1;
for(int i=2;i<=n;i++){
while(a[i].x-a[tmp].x > c){
S.erase(a[tmp]);
tmp++;
}
S.insert(a[i]);
pre=*--S.find(a[i]);
sub=*++S.find(a[i]);
if(pre.y!=-oo){
if(a[i].y-pre.y<=c){
merge(a[i].num,pre.num);
}
}
if(sub.y!=oo){
if(sub.y-a[i].y<=c){
merge(a[i].num,sub.num);
}
}
}
int maxn=0,cnt=0;
for(int i=1;i<=n;i++){
int f=getfa(a[i].num);
siz[f]++;
}
for(int i=1;i<=n;i++){
if(siz[i]) cnt++;
maxn=max(maxn,siz[i]);
}
printf("%d %d\n",cnt,maxn);
return 0;
}

【bzoj1604】【[Usaco2008 Open]Cow Neighborhoods】简单的谈谈曼哈顿距离的更多相关文章

  1. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...

  2. [BZOJ1604] [Usaco2008 Open] Cow Neighborhoods 奶牛的邻居 (queue & set)

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

  3. [BZOJ1604] [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(好题)

    传送门 良心题解 #include <set> #include <cstdio> #include <iostream> #include <algorit ...

  4. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 (Treap+单调队列)

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

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

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

  6. 【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000) ...

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

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

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

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

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

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

随机推荐

  1. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  2. Eclipse CDT 调用printf/cout 控制台(console)无输出

    转摘自:http://blog.csdn.net/dj0379/article/details/6940836 症状描述: 用Eclipse调试程序,执行printf和cout函数,但是console ...

  3. Hibernate 三种状态变化 与 sql 语句的关系

    前言:在Hibernate中有三种状态,对它的深入理解,才能更好的理解hibernate的运行机理,刚开始不太注意这些概念,后来发现它是重要的.对于理解hibernate,JVM和sql的关系有更好的 ...

  4. HDU1596 find the safest road---(最短路径dijkstra,#变形#)

    http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析: 题目要找一条安全度最高的路,安全度计算方法    Safe(P) = s(e1)*s(e2)…*s ...

  5. Ubuntu下kafka集群环境搭建及测试

    kafka介绍: Kafka[1是一种高吞吐量[2]  的分布式发布订阅消息系统,有如下特性: 通过O(1)的磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能 ...

  6. 【洛谷 P1712】 [NOI2016]区间 (线段树+尺取)

    题目链接 emmm看起来好像无从下手, \(l_i,r_i\)这么大,肯定是要离散化的. 然后我们是选\(m\)个区间,我们先对这些区间按长度排个序也不影响. 排序后,设我们取的\(m\)个区间的编号 ...

  7. python脚本运行的几种方式

    1.脚本式编程 将如下代码拷贝至 hello.py文件中: print ("Hello, Python!"); 通过以下命令执行该脚本: $ python ./hello.py h ...

  8. python 实现定时循环触发某个方法

    直接贴上代码 import threading def sayhello(): print "hello world" global t #Notice: use global v ...

  9. vnc无法显示桌面

    转载   以下是我的正确配置,解决上述问题,附带说明:  修改后的~/.vnc/xstartup #!/bin/sh # Uncomment the following two lines for n ...

  10. Kuangbin 带你飞 KMP扩展KMP Manacher

    首先是几份模版 KMP void kmp_pre(char x[],int m,int fail[]) { int i,j; j = fail[] = -; i = ; while (i < m ...