[BZOJ1604] [Usaco2008 Open] Cow Neighborhoods 奶牛的邻居 (queue & set)
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.
HINT
Source
Solution
设点$(x',y')$表示$(x+y,x-y)$,则:
有这样一个性质:两个点的曼哈顿距离为$max(|x'_1-x'_2|,|y'_1-y'_2|)$
所以先将所有点预处理,之后按$x'$坐标排序。
用一个队列维护$x'$使队列里所有元素$x'$之差不超过$C$,用set维护$y'$
若新插入元素的前驱或后继与该元素之差小于等于$C$就用并查集把它们连起来
数一下联通块的个数就行了。
#include <bits/stdc++.h>
#define fir first
#define sec second
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
const int INF = ;
set<pii> S;
set<pii>::iterator it;
queue<int> Q;
pii a[];
int fa[], siz[]; int getfa(int x)
{
return fa[x] = x == fa[x] ? x : getfa(fa[x]);
} int main()
{
int n, c, u, v, cnt = , mx = ;
cin >> n >> c;
for(int i = ; i <= n; i++)
{
cin >> a[i].fir >> a[i].sec;
a[i].fir += a[i].sec;
a[i].sec = a[i].fir - * a[i].sec;
fa[i] = i;
}
sort(a + , a + n + );
S.insert(mp(-INF, )), S.insert(mp(INF, ));
Q.push(), S.insert(mp(a[].sec, ));
for(int i = ; i <= n; i++)
{
while(!Q.empty() && a[i].fir - a[Q.front()].fir > c)
{
u = Q.front(), Q.pop();
S.erase(mp(a[u].sec, u));
}
it = S.lower_bound(mp(a[i].sec, ));
if(it->sec && it->fir - a[i].sec <= c)
{
u = getfa(it->sec), v = getfa(i);
if(u != v) fa[u] = v;
}
if((--it)->sec && a[i].sec - it->fir <= c)
{
u = getfa(it->sec), v = getfa(i);
if(u != v) fa[u] = v;
}
Q.push(i), S.insert(mp(a[i].sec, i));
}
for(int i = ; i <= n; i++)
{
if(++siz[getfa(i)] == ) cnt++;
mx = max(mx, siz[getfa(i)]);
}
cout << cnt << ' ' << mx << endl;
return ;
}
[BZOJ1604] [Usaco2008 Open] Cow Neighborhoods 奶牛的邻居 (queue & set)的更多相关文章
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 (Treap+单调队列)
题面 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi( ...
- [BZOJ1604] [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(好题)
传送门 良心题解 #include <set> #include <cstdio> #include <iostream> #include <algorit ...
- 【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000) ...
- BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
题目 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Time Limit: 5 Sec Memory Limit: 64 MB Description ...
- bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...
- 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1604 这题太神了... 简直就是 神思想+神做法+神stl.. 被stl整的我想cry...首先,, ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...
- BZOJ1604 & 洛谷2906:[USACO2008 OPEN]Cow Neighborhoods 奶牛的邻居——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1604 https://www.luogu.org/problemnew/show/P2906#sub ...
随机推荐
- redis 设置认证密码
通过修改redis配置文件(redis.conf)修改验证密码.(重启生效) requirepass myRedis 客户端通过密码登录redis并进行操作 redis-cli -h 127.0.0. ...
- SSE图像算法优化系列十七:多个图像处理中常用函数的SSE实现。
在做图像处理的SSE优化时,也会经常遇到一些小的过程.数值优化等代码,本文分享一些个人收藏或实现的代码片段给大家. 一.快速求对数运算 对数运算在图像处理中也是个经常会遇到的过程,特备是在一些数据压缩 ...
- 树莓派系列教程:1.环境与系统,无显示器无键盘无网线联网并使用PuTTy与VNC图形界面远程登录
本文所需物品清单: Raspberry Pi 3 Model B 主板.SD卡与读卡器(用于烧录系统) 资料整理来源在文尾 需要下载的资源与工具: 推荐系统-Raspbian 树莓派官方深度定制的硬件 ...
- RHEL6误安装RHEL7的包导致glibc被升级后系统崩溃处理方法
RHEL6误使用了RHEL7的光盘源,安装了某个RPM包之后,导致glibc被升级,进而导致系统崩溃. [root@rhel65 ~]# yum install ftp Loaded plugin ...
- C 语言中模板的几种实现方式
简单宏定义实现 简单宏定义 - 方式一 这种方式将主要实现部分放在一个宏定义中,利用字符替换的方式实现不同 type 的运算,详细思路见代码: simple_macro_1.c #include &l ...
- eclipse调试hadoop2.2.0源码笔记
在hadoop1.x版本时使用的是在Windows下编译Eclipse插件,远程调试集群.换成2.2.0,没有eclipse-plugin文件. hadoop2.2.0"远程调试集群&quo ...
- 通用的Android控件抖动效果实现
这个小程序的功能在实际的开发中会用到,比如:设置Button左右晃动,或者上下的晃动效果,下面就给出示例代码. 首先:要定义一个xml文件,命名为Shake [html] view plain cop ...
- php5.3命名空间内使用 php内置类的时候
在命名空间内使用内置类库的时候,需要使用 \ 比如 $zip =new \ZipArchive;
- FusionCharts 2D帕累托图
1.了解帕累托图的特性以及和其他图的共性 2.设计帕累托图页面中引入图的类型以及怎么引入到页面 Pareto2D.html: <!DOCTYPE HTML PUBLIC "-//W3C ...
- Windows 7 蓝屏原因
Windows 7 蓝屏 Microsoft (R) Windows Debugger Version 6.11.0001.404 X86 Copyright (c) Microsoft Corpor ...