bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+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.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
const int M=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,c;
int f[M],h[M],ans,mx;
int find(int x){while(f[x]!=x) x=f[x]=f[f[x]]; return x;}
struct node{
int x,y,id;
bool operator <(const node& k)const{return x<k.x;}
}e[M];
struct pos{
int y,id;
bool operator <(const pos& k)const{return y!=k.y?y<k.y:id<k.id;}
};
std::queue<int>q;
std::multiset<pos>tr;
std::multiset<pos>::iterator it;
int main(){
int x,y;
n=read(); c=read();
for(int i=;i<=n;i++){
x=read(); y=read();
f[i]=i; e[i].x=x+y,e[i].y=x-y; e[i].id=i;
}
sort(e+,e++n);
for(int i=;i<=n;i++){
while(!q.empty()&&e[i].x-e[q.front()].x>c){
int now=q.front(); q.pop();
tr.erase(tr.find((pos){e[now].y,e[now].id}));
}
q.push(i);
it=tr.insert((pos){e[i].y,e[i].id});
if(it!=tr.begin()){
--it;
if(e[i].y-(it->y)<=c){
int p=find(e[i].id),q=find(it->id);
f[q]=p;
}
++it;
}
++it;
if(it!=tr.end()){
if(it->y-e[i].y<=c){
int p=find(e[i].id),q=find(it->id);
f[q]=p;
}
}
}
for(int i=;i<=n;i++){
int x=find(e[i].id);
if(!h[x]) ans++;
h[x]++; mx=max(mx,h[x]);
}printf("%d %d\n",ans,mx);
return ;
}
bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set的更多相关文章
- 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 奶牛的邻居:队列 + multiset + 并查集【曼哈顿距离变形】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1604 题意: 平面直角坐标系中,有n个点(n <= 100000,坐标范围10^9) ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 曼哈顿生成树
大致题意:统计平面上由曼哈顿距离小于等于c的点对组成联通块的个数. 曼哈顿生成树的模板题.有关讲解:http://blog.csdn.net/acm_cxlove/article/details/88 ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居【切比雪夫距离+并查集+multiset】
参考:http://hzwer.com/4361.html 坐标开long long,inf开大点 先曼哈顿转切比雪夫(x+y,x-y),距离就变成了max(x',y'): 先按x排序,维护两个指针, ...
- BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap
题意:链接 方法: Treap 解析: 前几道资格赛的题水的不行,这道Gold的题就够分量辣. 首先这个曼哈顿距离啥的肯定能做文章,怎么转化是个问题,自己玩了一会没玩出来,就查了查曼哈顿距离的转化,发 ...
- 【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 奶牛的邻居
[算法]并查集+平衡树+数学+扫描线 [题解] 经典曼哈顿距离转切比雪夫距离. 曼哈顿距离:S=|x1-x2|+|y1-y2|<=c 即:max(x1-x2+y1-y2,x1-x2-y1+y2, ...
- bzoj 1623: [Usaco2008 Open]Cow Cars 奶牛飞车【排序+贪心】
从小到大排个序,然后能选就选 #include<iostream> #include<cstdio> #include<algorithm> using names ...
随机推荐
- SpringBoot学习:使用logback进行日志记录
项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 (一)pom.xml文件中引入jar: <!-- https://mvnrepos ...
- 从传统IT快速走向公共云计算
2年前有篇报道,说Facebook的每个运维同学至少能管理2万台服务器,这在当时的国内互联网引起了很大震动,按照传统IT的理解,每个运维同学能管理200台服务器已经很了不起了. 这些年来云计算发展非常 ...
- shell语句for循环
一:常用格式 格式一 for 变量 do 语句 done 格式二 for 变量 in 列表 do 语句 done 格式三 for ((变量=初始值; 条件判断; 变量变化)) do 语句 done 二 ...
- 使用IMAGEMAGICK的CONVERT工具批量转换图片格式
使用IMAGEMAGICK的CONVERT工具批量转换图片格式 http://www.qiansw.com/linux-imagemagick-convert-img.html Home > 文 ...
- SQLAlchemy技术文档(中文版)(上)
在学习SQLAlchemy的过程中,好多时候需要查官方Tutorial,发现网上并没有完整的中文版,于是利用这两天空余时间粗略翻译了一下. 翻译效果很差....但也算是强迫自己通读一遍Tutorial ...
- HTML5调用手机摄像头,仅仅支持OPPOHD浏览器
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Hibernate查询语言——HQL
HQL(Hibernate Query Language)查询语言是完全面向对象的查询语言,它提供了更加面向对象的封装,可以理解如多态.继承和关联. HQL的基本语法如下: select " ...
- [洛谷P1407][国家集训队]稳定婚姻
题目大意:有$n$对夫妻和$m$对情人,如果一对情人中的两人都离婚了,那么他们可以结为夫妻.对于每一对夫妻,若他们离婚后所有人依然可以结婚,那么就是不安全的,否则是安全的.问每一对夫妻是否安全. 题解 ...
- [POJ1631]Bridging signals
题目大意:不知,根据样例猜测为最长上升子序列(竟然还对了) 题解:$O(n log_2 n)$,求二维偏序,(q为存答案的序列,a存原序列,len为答案) for(int i = 1; i <= ...
- AtCoder Grand Contest 025 Problem D - Choosing Points
题目大意:输入$n,d1,d2$,你要找到$n^2$个整点 x, y 满足$0 \leqslant x, y<2n$.并且找到的任意两个点距离,既不是$\sqrt{d1}$,也不是 $\sqrt ...