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)只奶牛,你会发 ...
随机推荐
- loadRunner 11.0 安装及破解
http://jingyan.baidu.com/article/20095761b31b58cb0621b463.html 破解时必须是管理员账户登录.
- Grunt的配置和使用(一)
Grunt的配置和使用(一) Grunt 和 Grunt 的插件都是通过 Node.js 的包管理器 npm 来安装和管理的.为了方便使用 Grunt ,你应该在全局范围内安装 Grunt 的命令行接 ...
- !!!易控INSPEC组态软件开发小结——-一次工程文件损坏和处理经过
从加入红橡开始熟悉和使用易控(INSPEC)组态软件,值得赞扬的是INSPEC的开放性和对C#语言的支持,除此之外,便也没有感觉它与其他组态软件有太多优势,有人说INSPEC软件授权比国内其他同类的组 ...
- If We Were a Child Again
Description The Problem The first project for the poor student was to make a calculator that can jus ...
- 提高PHP编程效率
1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row['id']的速度是$row[id]的7倍. 3.echo比print快,并且使用echo的多重 ...
- mysql server5.6.28 修改数据目录
1.查看配置文件 mysql --help | grep my.cnf 列出使用哪个配置文件(顺序推) 2.service mysql stop 3.创建新目录 mkdir /data 4.迁移之前的 ...
- fork出的子进程和父进程的继承关系【转载】
[原文地址]http://blog.163.com/dengjingniurou@126/blog/static/53989196200962924412524/ fork出的子进程和父进程的继承关系 ...
- 高质量程序设计指南C/C++语言——内存管理
• free()和delete只是把指针所指的内容给释放掉,并没有把指针本身删掉.指针被free()或delete以后其地址仍然不变(不等于NULL),只是该地址对应的内存是垃圾——p成了野指针.如果 ...
- Spring 基于注解的装配
xml头文件 xmlns:context="http://www.springframework.org/schema/context" 扫描包:<context:compo ...
- 安装 Rational Rose 启动报错:无法启动此程序,因为计算机中丢失 suite objects.dll
安装完以后提示找不到 suite objects.dll: 经查找,该 dll 存在: 找不到的原因是,安装程序自动设置在 Path 中的环境变量有误: 把最后的 common 改成 Common: ...