【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集
【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
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.
题解:曼哈顿距离不是太好搞,我们先将坐标系旋转45°,即每个点的坐标变为(x+y,y-x),就变成了切比雪夫距离,即对于每个牛,与它相邻的牛都在一个正方形范围内。
于是我们将所有点按x排序,枚举第i个点,用Treap维护x在[xi-C,xi]中的所有点,每次在Treap找出所有y在[yi-C,yi+C]中的所有点,将他们所在的并查集都与i合并。但是这样的点太多了,不过我们其实只需要找出yi的前驱和后继。因为如果存在yk>yj>yi,且xk,xj∈[xi-C,xi],那么xk和xj一定已经被合并了,不需要重复计算。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <utility>
#define mp(A,B) make_pair(A,B)
using namespace std;
const int maxn=100010;
int n,C,root,sum,ans,tot,pre,nxt;
int f[maxn],siz[maxn],vis[maxn];
typedef pair<int,int> pii;
struct node
{
int x,y;
}p[maxn];
struct treap
{
int key,ch[2];
pii v;
}tr[maxn];
int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-')f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
int find(int x)
{
return (f[x]==x)?x:(f[x]=find(f[x]));
}
bool cmp(node a,node b)
{
return a.x<b.x;
}
void rotate(int &x,int d)
{
int y=tr[x].ch[d];
tr[x].ch[d]=tr[y].ch[d^1],tr[y].ch[d^1]=x,x=y;
}
void insert(int &x,pii y)
{
if(!x)
{
x=++tot,tr[x].v=y,tr[x].ch[0]=tr[x].ch[1]=0,tr[x].key=rand()*rand();
return ;
}
int d=(tr[x].v<y);
insert(tr[x].ch[d],y);
if(tr[tr[x].ch[d]].key>tr[x].key) rotate(x,d);
}
void del(int &x,pii y)
{
if(tr[x].v==y)
{
if(tr[x].ch[0]*tr[x].ch[1]==0) x=tr[x].ch[0]^tr[x].ch[1];
else
{
int d=tr[tr[x].ch[0]].key<tr[tr[x].ch[1]].key;
rotate(x,d),del(tr[x].ch[d^1],y);
}
return ;
}
del(tr[x].ch[tr[x].v<y],y);
}
void getpre(int x,int y)
{
if(!x) return ;
if(tr[x].v.first<=y) pre=x;
getpre(tr[x].ch[tr[x].v.first<=y],y);
}
void getnxt(int x,int y)
{
if(!x) return ;
if(tr[x].v.first>=y) nxt=x;
getnxt(tr[x].ch[tr[x].v.first<y],y);
}
int main()
{
srand(2333666);
n=rd(),C=rd();
int i,j,a,b;
for(i=1;i<=n;i++) a=rd(),b=rd(),p[i].x=a+b,p[i].y=b-a,f[i]=i,siz[i]=1;
sort(p+1,p+n+1,cmp);
for(i=j=1;i<=n;i++)
{
for(;p[i].x-p[j].x>C;j++) del(root,mp(p[j].y,j));
pre=0,getpre(root,p[i].y);
if(pre&&p[pre].y>=p[i].y-C)
if(find(pre)!=find(i)) siz[f[i]]+=siz[f[pre]],f[f[pre]]=f[i];
nxt=0,getnxt(root,p[i].y);
if(nxt&&p[nxt].y<=p[i].y+C)
if(find(nxt)!=find(i)) siz[f[i]]+=siz[f[nxt]],f[f[nxt]]=f[i];
insert(root,mp(p[i].y,i));
}
for(i=1;i<=n;i++) if(find(i)==i) sum++,ans=max(ans,siz[i]);
printf("%d %d\n",sum,ans);
return 0;
}
【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集的更多相关文章
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 (Treap+单调队列)
题面 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi( ...
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...
- [BZOJ1604] [Usaco2008 Open] Cow Neighborhoods 奶牛的邻居 (queue & set)
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...
- [BZOJ1604] [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(好题)
传送门 良心题解 #include <set> #include <cstdio> #include <iostream> #include <algorit ...
- BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap
题意:链接 方法: Treap 解析: 前几道资格赛的题水的不行,这道Gold的题就够分量辣. 首先这个曼哈顿距离啥的肯定能做文章,怎么转化是个问题,自己玩了一会没玩出来,就查了查曼哈顿距离的转化,发 ...
- BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
题目 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Time Limit: 5 Sec Memory Limit: 64 MB Description ...
- BZOJ1604 & 洛谷2906:[USACO2008 OPEN]Cow Neighborhoods 奶牛的邻居——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1604 https://www.luogu.org/problemnew/show/P2906#sub ...
- 【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 旋转坐标系+并查集+Treap/STL-set
题目描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤ ...
- bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...
随机推荐
- [Machine Learning with Python] My First Data Preprocessing Pipeline with Titanic Dataset
The Dataset was acquired from https://www.kaggle.com/c/titanic For data preprocessing, I firstly def ...
- Xamarin XAML语言教程Visual Studio中实现XAML预览
Xamarin XAML语言教程Visual Studio中实现XAML预览 每次通过编译运行的方式查看XAML文件效果,需要花费大量的时间.如果开发者使用XAML对UI进行布局和设计,可以通过预览的 ...
- luogu P3800 Power收集
题目背景 据说在红雾异变时,博丽灵梦单身前往红魔馆,用十分强硬的手段将事件解决了. 然而当时灵梦在Power达到MAX之前,不具有“上线收点”的能力,所以她想要知道她能收集多少P点,然而这个问题她答不 ...
- springBoot 整合 mybatis+Oracle
现在比较流行的操作数据库操作层框架Mybatis,下面我们就来看看Springboot如何整合mybatis, 之前一直在用xml形式写sql,这次依然用xml的方式感觉这种还是比较灵活方便. 添加m ...
- eclipse 国际化 $NON-NLS-1$ 含义
一.$NON-NLS-1$ 含义 Eclipse 如果每行代码里有这个字符串:$NON-NLS-1$ 表示:这一行的第一个字符串是不需要国际化的.同理$NON-NLS-2$,$NON-NLS-3$.. ...
- ArcGIS 10.2 二次开发,兼容Visual Studio 2012二次开发,完美安装教程
GIS 经常安装是常有的事,每次重装系统都要浪费大半天去安装这个.所以凑这一次安装,把这个软件重新安装的步骤整理了一下,希望对大家有所帮助.这次整理的内容的关键优点是,对常见的出错内容进行了归纳整理. ...
- Form元素示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Windows2003建立FTP服务器以及报530 User <用户名> cannot log in home directory inaccessible的解决方法
Windows2003建立FTP服务器: Windows2003建立FTP服务器 FTP连接 ...
- 关于Web应用和容器的指纹收集以及自动化软件的制作
一次对Web应用的渗透,九成都是从信息收集开始,所以信息收集就显得尤为重要.关键信息的收集可以使你在后期渗透的时候更加的得心应手,把渗透比喻成走黑暗迷宫的话,那信息收集可以帮你点亮迷宫的大部分地图. ...
- Drools环境搭建
Eclipse3.5安装Drools6.5.0.Final插件 到Drools下载页面(现在是http://www.jboss.org/drools/downloads.html) -下载并解压Dro ...