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)只奶牛,你会发 ...
随机推荐
- Cocos2d-x3.0游戏实例之《别救我》第四篇——乱入的主角
好了,前面说了那么多废话,最终要进入正题了(等等,敢情前面你都是在耍我们么?) 笨木头花心贡献,啥?花心?不呢,是用心~ 转载请注明,原文地址: http://www.benmutou.com/blo ...
- Java中布尔类型操作符&=,|=与^=的使用
今天在对同事的代码进行code review的时候,见到一个比较好玩的写法.“flag &= false:”,乍一看,还感觉他写错了,但是程序可以正常运行,赶紧去百度,看一下这个写法到底是怎么 ...
- 关于两次指针(struct型)传参数的问题
这两天被struct传参给郁闷死了.今天终于解决了. 比如有一个struct如下: struct _ns1__Add_USCORESensorDataArray{ struct xsd__base64 ...
- hdu 1251 统计难题(trie 树的简单应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给你多个字符串,求以某个字符串为前缀的字符串数量. 思路:简单的trie数应用,在trie ...
- python 10 min系列三之小爬虫(一)
python10min系列之小爬虫 前一篇可视化大家表示有点难,写点简单的把,比如命令行里看论坛的十大,大家也可以扩展为抓博客园的首页文章 本文原创,同步发布在我的github上 据说去github右 ...
- 10-C语言函数
目录: 一.函数 二.return与exit关键字 三.递归与递推 回到顶部 一.函数 1 函数由函数名.返回值.形参.函数体组成. 函数的使用分三个步骤:声明.定义.调用 2 语法格式: 返回值类型 ...
- 转: vim简明教程
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- JS声明语句提升与作用域
<!DOCTYPE html><html><head></head><body><script>//-------------- ...
- Protel中放置汉字工具的使用图示
首先先到网上下载Protel中放置汉字工具ProtelHz.然后把ProtelHz中的文件全部解压到Protel99se安装目录X:\Program Files\Design Explorer 99 ...
- 在IT公司,project manager 基本上和秘书,助理什么的差不多
我感觉非常有道理,所以我不做Leader,哈哈哈