BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
题目
1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
Time Limit: 5 Sec Memory Limit: 64 MB
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.
题解
这题很明显就是再考并查集!【真的吗,你看下数据范围!】关于曼哈顿距离的技巧,将每个点变成(x+y,x-y)这样两个点之间的曼哈顿距离就是|x1-x2|+|y1-y2|,这样就可以维护一个平衡树,根据前驱和后继上面的节点来维护并查集求得答案了!
代码
/*Author:WNJXYK*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
using namespace std; #define LL long long
#define Inf 2147483647
#define InfL 10000000000LL inline void swap(int &x,int &y){int tmp=x;x=y;y=tmp;}
inline void swap(LL &x,LL &y){LL tmp=x;x=y;y=tmp;}
inline int remin(int a,int b){if (a<b) return a;return b;}
inline int remax(int a,int b){if (a>b) return a;return b;}
inline LL remin(LL a,LL b){if (a<b) return a;return b;}
inline LL remax(LL a,LL b){if (a>b) return a;return b;} inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,c,ans,mx;
int fa[100005],tot[100005];
struct data{LL x,y;int id;}a[100005];
multiset <data> b;
set <data>::iterator it;
inline bool operator<(data a,data b){
return a.y<b.y;
}
inline bool cmpx(data a,data b){
return a.x<b.x;
}
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
inline void un(int x,int y){
int p=find(x),q=find(y);
if(p!=q){
fa[p]=q;
ans--;
}
}
void solve(){
b.insert((data){0,InfL,0});b.insert((data){0,-InfL,0});
int now=1;b.insert(a[1]);
for(int i=2;i<=n;i++){
while(a[i].x-a[now].x>c){
b.erase(b.find(a[now]));
now++;
}
it=b.lower_bound(a[i]);
data r=*it,l=*--it;
if(a[i].y-l.y<=c)
un(a[i].id,l.id);
if(r.y-a[i].y<=c)
un(a[i].id,r.id);
b.insert(a[i]);
}
}
int main(){
n=read();c=read();ans=n;
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=1;i<=n;i++){
int t1=read(),t2=read();
a[i].x=t1+t2,a[i].y=t1-t2;a[i].id=i;
}
sort(a+1,a+n+1,cmpx);
solve();
for(int i=1;i<=n;i++)
tot[find(i)]++;
for(int i=1;i<=n;i++)
mx=max(mx,tot[i]);
printf("%d %d\n",ans,mx);
return 0;
}
BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居的更多相关文章
- bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...
- 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, ...
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...
随机推荐
- sql语句开发使用---update
单表的更新大家都用过了,现在说下实际开发过程中,需要多表的查询更新状态,或者跨数据库的更新状态. 东西需要学习了才会懂得,所以站在巨人的肩膀看的更远. sql 语法; UPDATE 表名称 SET 列 ...
- hdu 2191悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)
#include<iostream> #include<cstdio> #include<algorithm> /* 虽然该题不排序也可以过,但是我认为价格和重量最 ...
- sql--关于exec和sp_execute
sql:exec与sp_excutesql的比较 exec与sp_execute都可以执行存储过程和批处理动态sql语句,以下所属均是关于批处理动态sql语句方面. 一.关于输入参数与输出参数 1.使 ...
- window 7 改变窗口颜色
对于刚刚重新安装的window7系统的盆友来说,不能改变窗口的颜色,和别人的window7窗口颜色和样子不一样和不好玩,那么我教大家,如何更改窗口颜色. 首先,重装的系统用软件激活之后,肯定还没还得急 ...
- 关于TableViewCell高度自适应问题的整理
TableViewCell高度自适应在网上有很多资料,我只想找出最最最简单的一种方法. 首先梳理一下思路.说到TableViewCell我们第一个想到的问题或许就是cell的复用问题. 1. [se ...
- EasyUI的使用步骤
(1) 将easyui-1.4.3中jquery.min.js\jquery.easyui.min.js复制到工程的script下 (2) 将themes复制到工程中 (3) 在页面中引入2个JS 2 ...
- Python之路Day2
-->the start 养成好习惯,每次上课的内容都要写好笔记. 第二天内容主要是熟悉int.long.float.str.list.dict.tuple这几个类的内建方法. 对于Python ...
- SIGAR - System Information Gatherer And Reporter
https://support.hyperic.com/display/SIGAR/Home 收藏一篇: http://www.cnitblog.com/houcy/archive/2012/11/2 ...
- linux下c/c++方式访问curl的帮助手册
自:http://blog.chinaunix.net/u1/47395/showart_1768832.html 有个业务需求需要通过curl 代理的方式来访问外网 百度了一把,测试可以正常使用.记 ...
- PHP面试题汇总参考
PHP面试题汇总 这是一份比较全面的PHP面试题.对准备去新公司应聘PHP职位的开发者应该有帮助.或者说,对招聘PHP开发人员的企业也有些帮助,不过就不要原样打印出来考了,稍微改一改. 简述题(50分 ...