题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1604

题意:

  平面直角坐标系中,有n个点(n <= 100000,坐标范围10^9)。

  给定r,当两个点的曼哈顿距离<=r时,认为这两个点在同一个“群”中。

  问你共有多少个群,以及点的数量最多的群有多少个点。

题解:

  本题的核心在于:如何枚举一个点周围满足“曼哈顿距离<=r”的点。

  由于 曼哈顿距离 = |x1 - x2| + |y1 - y2|。

  x和y相互影响,不能单纯按x或y排序,枚举所有点总复杂度为O(N^2)。

  所以要用到曼哈顿距离的另一种形式:

    设X = x + y , Y = x - y

    d(曼哈顿距离) = max(|X1-X2|, |Y1-Y2|)

  将每个点的X = x + y,Y = x - y,这就将X与Y的关系分离开了。

  

  将所有点按X排序。

  当前考虑到点i。

  用一个队列(X升序),保证队列中的所有X满足要求,否则不断删去队首。

  用一个multiset(Y升序),找到i的前驱pre和后继suc,如果i与pre(或suc)的Y满足要求,则合并(并查集)。

  最后统计一下每个群就好了。

AC Code:

 // |x1-x2| + |y1-y2|
// | (x1+y1) - (x2+y2) |
// | (x1-y1) - (x2-y2) |
// X = x + y
// Y = x - y
// d = max(|X1-X2|, |Y1-Y2|) <= r
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#define MAX_N 100005
#define INF 100000000 using namespace std; struct Coor
{
int x;
int y;
int idx;
Coor(int _x,int _y,int _idx)
{
x=_x;
y=_y;
idx=_idx;
}
Coor(){}
friend bool operator < (const Coor &a,const Coor &b)
{
return a.y!=b.y?a.y<b.y:a.idx<b.idx;
}
}; int n,r;
int ans=;
int counter=;
int head=;
int par[MAX_N];
int cnt[MAX_N];
Coor c[MAX_N];
multiset<Coor> mst; inline bool cmp_x(const Coor &a,const Coor &b)
{
return a.x<b.x;
} void init_union_find()
{
for(int i=;i<n;i++)
{
par[i]=i;
}
} int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);
} void unite(int x,int y)
{
int px=find(x);
int py=find(y);
if(px==py) return;
par[px]=py;
} void read()
{
cin>>n>>r;
int a,b;
for(int i=;i<n;i++)
{
cin>>a>>b;
c[i].x=a+b;
c[i].y=a-b;
c[i].idx=i;
}
} void solve()
{
init_union_find();
sort(c,c+n,cmp_x);
mst.insert(Coor(,INF,));
mst.insert(Coor(,-INF,));
mst.insert(c[head]);
for(int i=;i<n;i++)
{
while(c[i].x-c[head].x>r)
{
mst.erase(c[head]);
head++;
}
multiset<Coor>::iterator it=mst.lower_bound(c[i]);
Coor suc=*it;
Coor pre=*--it;
if(c[i].y-pre.y<=r) unite(pre.idx,c[i].idx);
if(suc.y-c[i].y<=r) unite(suc.idx,c[i].idx);
mst.insert(c[i]);
}
memset(cnt,,sizeof(cnt));
for(int i=;i<n;i++)
{
int p=find(i);
if(cnt[p]==) counter++;
cnt[p]++;
ans=max(ans,cnt[p]);
}
} void print()
{
cout<<counter<<" "<<ans<<endl;
} int main()
{
read();
solve();
print();
}

BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居:队列 + multiset + 并查集【曼哈顿距离变形】的更多相关文章

  1. bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...

  2. 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1604 这题太神了... 简直就是 神思想+神做法+神stl.. 被stl整的我想cry...首先,, ...

  3. BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    题目 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Time Limit: 5 Sec  Memory Limit: 64 MB Description ...

  4. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...

  5. 【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 旋转坐标系+并查集+Treap/STL-set

    题目描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤ ...

  6. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 曼哈顿生成树

    大致题意:统计平面上由曼哈顿距离小于等于c的点对组成联通块的个数. 曼哈顿生成树的模板题.有关讲解:http://blog.csdn.net/acm_cxlove/article/details/88 ...

  7. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居【切比雪夫距离+并查集+multiset】

    参考:http://hzwer.com/4361.html 坐标开long long,inf开大点 先曼哈顿转切比雪夫(x+y,x-y),距离就变成了max(x',y'): 先按x排序,维护两个指针, ...

  8. BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap

    题意:链接 方法: Treap 解析: 前几道资格赛的题水的不行,这道Gold的题就够分量辣. 首先这个曼哈顿距离啥的肯定能做文章,怎么转化是个问题,自己玩了一会没玩出来,就查了查曼哈顿距离的转化,发 ...

  9. 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [算法]并查集+平衡树+数学+扫描线 [题解] 经典曼哈顿距离转切比雪夫距离. 曼哈顿距离:S=|x1-x2|+|y1-y2|<=c 即:max(x1-x2+y1-y2,x1-x2-y1+y2, ...

随机推荐

  1. js中推断浏览器类型

    在实际看发展.有时候会遇到在IOS和Android中要用不同的方法处理网页.须要让网页返回当前浏览器的类型. /** * 推断浏览器类型 */ var Browse = function () { / ...

  2. 以前整理的网络上免费API接口

    以前整理的一些免费的API接口,具体是否好用还需要时间测试,但是先分享给大家. 天气接口 聚合数据: http://op.juhe.cn/onebox/weather/query 用例 官方文档 来源 ...

  3. 微信开源组件WCDB漫谈及Demo

    代码地址如下:http://www.demodashi.com/demo/12422.html 前言 移动端的数据库选型一直是一个难题,直到前段时间看到了WeMobileDev(微信前端团队)放出了第 ...

  4. 原生domReady封装

    核心思路: 标准浏览器(含IE9+)比较简单,直接监听DOMContentLoaded事件: 低版本的IE(IE678)两套机制: 1)尝试轮询document.documentElement.doS ...

  5. Enumerate Combination C(k, n) in a bitset

    Suppose n<=32, we can enumerate C(k, n), with bits representing absence or presence, in the follo ...

  6. flash插件使用外部数据的方法

    使用xml保存需要改变的数据,如轮播图的图片路径,也可以在xml中指定数据库地址等

  7. Java 8 Collectors to Map

    1. 介绍 在本教程中,我们将讨论Collectors类的toMap()方法.我们使用它将流收集到一个Map实例中. 对于本教程中涉及的所有示例,我们将使用图书列表作为数据源,并将其转换为不同的Map ...

  8. Android Calendar的学习与运用

    [java]mport java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; ...

  9. Nginx性能测试

    环境:Centos 7.0  Nginx 1.6.2 测试工具:siege 3.0.7 配置1: I3-3110M 4G 测试1(100用户,1000请求): 平均响应:0.06s 并发数:59.19 ...

  10. sqlite3常用操作命令 和mysql的区别及优缺点

    SQLite 的数据库权限只依赖于文件系统,没有用户帐户的概念. sqlite3 testdb.db .databases 命令查看数据库列表 create table tbl(name char(1 ...