[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 ...
随机推荐
- 大型互联网公司Java开发岗位面试题归类!
一.Java基础 1. String类为什么是final的. 2. HashMap的源码,实现原理,底层结构. 3. 说说你知道的几个Java集合类:list.set.queue.map实现类咯.. ...
- 图论算法-最小费用最大流模板【EK;Dinic】
图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...
- mysql数据库外部无法访问
有以下两种情况: 1.mysql未分配访问权限 格式:grant 权限 on 数据库名.表名 用户@登录主机 identified by "用户密码"; grant select, ...
- Flag
之前一直怕别人知道你在干什么,其实根本没人在乎你在干什么.做好你自己不就行了,现在这里将成为你的记录生活与学习的地方,尽管目前就你自己,但是你要相信,会有千万人和你在一起,大胆的往前走吧.--- 致自 ...
- Java经典编程题50道之四十二
809*??=800*??+9*??+1,其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数.求??代表的两位数,以及809*??后的结果. public class Example ...
- HTML5VEDIO标签阿里云-微信浏览器兼容性问题
在网页展示媒体对象,离不开HTML5的 audio和video对象.但这两个目前来看兼容性方面还得关注一下. 目前在做一个阿里云下载video 并在微信端播放mp4格式的视频的时候,碰到了一些兼容性问 ...
- XP环境下的网络证书问题
项目过程中,由于是收银系统需要从服务器获取支付二维码,会产生SSL连接的问题,在win7.win10上都没有问题,放到WIN XP上出现了The underlying connection was c ...
- PHP结合Redis来限制用户或者IP某个时间段内访问的次数
$redis = new Redis(); $redis->connect('127.0.0.1', 6379); //获取客户端真实ip地址 function get_real_ip(){ s ...
- hdu 2045 递推
从n>=4开始考虑,只考虑n-1和1的颜色是否相等情况.推出公式F(n)=F(n-1)+2*F(n-2) AC代码: #include<cstdio> const int maxn= ...
- uva437 DAG
直接套用DAG的思路就行. AC代码: #include<cstdio> #include<cstring> #include<algorithm> using n ...