P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

考虑维护曼哈顿距离:$\left | x_{1}-x_{2} \right |+\left | y_{1}-y_{2} \right |$

看起来很难维护的样子,我们尝试转化

设两个点$(x_{1},y_{1}),(x_{2},y_{2})  (x_{1}>=x_{2})$

那么它们的曼哈顿距离有2种情况

1.$y_{1}>y_{2}:\left | x_{1}-x_{2} \right |+\left | y_{1}-y_{2} \right |=x_{1}-x_{2}+y_{1}-y_{2}=(x_{1}+y_{1})-(x_{2}+y_{2})$

2.$y_{1}<y_{2}:\left | x_{1}-x_{2} \right |+\left | y_{1}-y_{2} \right |=x_{1}-x_{2}-y_{1}+y_{2}=(x_{1}-y_{1})-(x_{2}-y_{2})$

于是我们就可以转为维护$(X=x+y,Y=x-y)$

这样曼哈顿距离就愉快地转化为$max(X_{1}-X_{2},Y_{1}-Y_{2})$了

我们先把所有坐标按$x$升序排一遍。

蓝后$x$坐标用一个队列维护

$y$坐标则用$multiset$维护(当然你愿意的话也可以打个平衡树(逃))

每次在$multiset$里搞搞啥$lower\_bound$操作就行了。

至于点的联通问题,搞一个并查集

end.

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#define re register
using namespace std;
typedef long long ll;
void read(ll &x){
char c=getchar();x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=(x<<)+(x<<)+(c^),c=getchar();
}
int max(int &a,int &b){return a>b?a:b;}
#define N 100002
struct data{
ll x,y;int id;
data(){}
data(ll A,ll B,int C):
x(A),y(B),id(C)
{}
bool operator < (const data &tmp) const{return y<tmp.y;}
}a[N];
bool cmp(const data &A,const data &B){return A.x<B.x;}
multiset <data> s;
int n,t,mxd=-1e9,fa[N],siz[N]; ll c;
int found(int x){return fa[x]==x?x:fa[x]=found(fa[x]);}
void uni(int x,int y){
int r1=found(x),r2=found(y);
if(r1!=r2){
siz[r1]+=siz[r2]; --t;
siz[r2]=; fa[r2]=r1;
}
}
int main(){
scanf("%d",&n); read(c); t=n; ll q1,q2;
for(re int i=;i<=n;++i){
read(q1); read(q2);
fa[i]=i; siz[i]=;
a[i]=data(q1+q2,q1-q2,i);
}sort(a+,a+n+,cmp);
s.insert(data(,1e16,));
s.insert(data(,-1e16,));//添加边界防止指针越界
s.insert(a[]); int hd=;
multiset<data>::iterator it;
for(re int i=;i<=n;++i){
while(a[hd].x+c<a[i].x) s.erase(s.find(a[hd++]));//队列维护
it=s.lower_bound(a[i]);//找到第一个>=a[i].y的
if((*it).y-a[i].y<=c) uni((*it).id,a[i].id);
if(a[i].y-(*(--it)).y<=c) uni((*it).id,a[i].id);
s.insert(a[i]);
}printf("%d ",t);
for(re int i=;i<=n;++i) mxd=max(mxd,siz[i]);
printf("%d",mxd);
return ;
}

bzoj1604 / P2906 [USACO08OPEN]牛的街区Cow Neighborhoods的更多相关文章

  1. 洛谷 P2906 [USACO08OPEN]牛的街区Cow Neighborhoods | Set+并查集

    题目: https://www.luogu.org/problemnew/show/P2906 题解: 垃圾水题 #include<cstdio> #include<algorith ...

  2. P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

    传送门 曼哈顿距离好像不好直接算,我们可以把牛的坐标转化一下以方便计算距离 (x,y) --> (x+y,x-y) 那么距离就可以表示成 $max(\left |x_1-x_2  \right ...

  3. 「洛谷P2906」[USACO08OPEN]牛的街区Cow Neighborhoods 解题报告

    P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 题目描述 Those Who Know About Cows are aware of the way cows gr ...

  4. [USACO08OPEN]牛的街区Cow Neighborhoods

    题目描述: luogu 题解: 技巧题. 曼哈顿距离:$|x1-x2|+|y1-y2|$ 切比雪夫距离:$\max(|x1-x2|,|y1-y2|)$ 曼哈顿距离转切比雪夫距离:$(x,y)-> ...

  5. bzoj1623 / P2909 [USACO08OPEN]牛的车Cow Cars

    P2909 [USACO08OPEN]牛的车Cow Cars 显然的贪心. 按速度从小到大排序.然后找车最少的车道,查询是否能填充进去. #include<iostream> #inclu ...

  6. 洛谷 P2909 [USACO08OPEN]牛的车Cow Cars

    传送门 题目大意: m个车道. 如果第i头牛前面有k头牛,那么这头牛的最大速度会 变为原本的速度-k*D,如果速度小于l这头牛就不能行驶. 题解:贪心 让初始速度小的牛在前面 代码: #include ...

  7. [USACO08OPEN]牛的车Cow Cars

    题目描述 N (1 <= N <= 50,000) cows conveniently numbered 1..N are driving in separate cars along a ...

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

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

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

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

随机推荐

  1. 当inline-block或者float失效的时候怎么弄

    当我们想要元素水平排列的时候,inline-block或者float是最好的选择了,但是当父元素的宽高都设置了的时候,子元素会失效,如下: 代码: <!DOCTYPE html> < ...

  2. iOS @synthesize var = _var 变量前置下划线解释

    本文转载至 http://blog.csdn.net/ztp800201/article/details/9231617  前置下划线是一种为了帮助区分实例变量和访问方法的约定.对于编译器来说它只是一 ...

  3. JAVA中替换字符的方法replace和replaceAll 区别

    replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是:1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSe ...

  4. VS2013快捷键

    这个好用,先放这两个 组合键“Ctrl+Enter”:在当前行的上面插入一个空行:  组合键“Ctrl+Shift+Enter”:在当前行的下面插入一个空行.

  5. 图论之最短路径(1)——Floyd Warshall & Dijkstra算法

    开始图论学习的第二部分:最短路径. 由于知识储备还不充足,暂时不使用邻接表的方法来计算. 最短路径主要分为两部分:多源最短路径和单源最短路径问题 多源最短路径: 介绍最简单的Floyd Warshal ...

  6. SDOI 2016 Round1 Day2

    生成魔咒 /* 后缀数组+双向链表 参照:https://blog.csdn.net/clove_unique/article/details/53911757 */ #include<cstd ...

  7. SRC常见WEB漏洞系列之HTTP-HOST头攻击

    一.背景: web程序需要知道网站的域名比较麻烦,需要使用HTTP的 host头字段: <?php _SERVER["HTTP_HOST"] ?> @app.route ...

  8. windows 下,go语言 交叉编译

    http://bbs.csdn.net/topics/390601048 参考上面的操作

  9. C++,一些常用的知识点

    用typedef定义数组.指针.结构等类型将带来很大的方便,不仅使程序书写简单而且使意义更为明确,因而增强了可读 性. C++中是用关键字typedef定义一个标识符来代表一种数据类型,该标识符可以象 ...

  10. Redis构建处理海量数据的大型购物网站

    本系列教程内容提要 Java工程师之Redis实战系列教程教程是一个学习教程,是关于Java工程师的Redis知识的实战系列教程,本系列教程均以解决特定问题为目标,使用Redis快速解决在实际生产中的 ...