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. 【WebService】Stax的基本操作基于游标

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <book ...

  2. EUI组件之DataGroup

    看官网教程,这个没法单独用. http://developer.egret.com/cn/github/egret-docs/extension/EUI/dataCollection/dataGrou ...

  3. Android TextView文字透明度和背景透明度设置

    textview1.setTextColor(Color.argb(255, 0, 255, 0)); //文字透明度 控件设为半透明: 控件名.getBackground().setAlpha(in ...

  4. java的list集合操作List<T>转化List<Long>

    java的list集合操作List<T>转化List<Long> package com.google.common.collect; import com.google.co ...

  5. 微信小程序 --- model弹框

    model弹框:在屏幕中间弹出,让你进行选择: 效果: 代码: <button type="primary" bindtap="btnclick"> ...

  6. nginx配置文件内容详情及基本属性配置

    Nginx的配置文件nginx.conf配置详解如下: user nginx nginx ; Nginx用户及组:用户 组.window下不指定 worker_processes 8; 工作进程:数目 ...

  7. python使用MySqlDB

    下载安装MySQLdb <1>linux版本 http://sourceforge.net/projects/mysql-python/ 下载,在安装是要先安装setuptools,然后在 ...

  8. Oracle之rman命令的使用(51CTO风哥rman课程)

    看rman的连接串的帮助 连接数据库 rman target/ rman的版本要和目标数据库一致(一般大版本可以往下兼容小版本) 运行操作系统命令 run {host "pwd"; ...

  9. 徐州网络赛A-Hard To Prepare【dp】【位运算】【快速幂】

    After Incident, a feast is usually held in Hakurei Shrine. This time Reimu asked Kokoro to deliver a ...

  10. 【react 条件渲染】在render的html中使用 三元运算符 进行条件渲染

    return ( {renderedPages.map(page => ( <Button key={page} onClick={() => onPageChange(page)} ...