题目链接: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. vue中使用key管理可复用的元素

    1.概述 Vue 会尽可能高效地渲染元素,通常会复用已有元素而不是从头开始渲染. key解决上述问题之外的情景:这两个元素是完全独立的,不要复用它们. 2.示例 <!DOCTYPE html&g ...

  2. mongodb:monogo和php整合

    1.到如下网址,下载php扩展包,找一个最新stable版的.

  3. Notepad2替换记事本--映像劫持

     Image File Execution Options就是映像劫持技术,通过此种方式替换记事本,非常地绿色环保. Image File Execution Options是CreateProces ...

  4. JVM内存最大能调多大分析【经典】

       http://hi.baidu.com/suofang/blog/item/49c637c71c0afbd0d1006028.html  上次用weblogic 把 -XmxXXXX 设成2G, ...

  5. Javascript模式(一) 单例模式

    function A(){ // 存储实例对象 var instance; // 重写构造函数,只返回闭包内的局部变量instance A = function(){ return instance; ...

  6. oracle 表压缩技术

    压缩表是我们维护管理中常常会用到的.以下我们看都oracle给我们提供了哪些压缩方式. 文章摘自"Oracle® Database Administrator's Guide11g Rele ...

  7. items" does not support runtime expression

    <%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%> 更改为  <%@tagl ...

  8. Layout布局位置

    - - GUILayout 这个本身就是用于自动布局的. 不用给定位置的,这也是它与GUI的区别所在.通常默认开始的位置是屏幕的左上角. 当然你也可以限定开始自动布局的位置.用 GUILayout.B ...

  9. access变转换为mysql表工具

    1.一个是国外软件,名字叫Access2MySQL,下载地址:http://www.pc6.com/softview/SoftView_7187.html 2.第二款软件是月光博客写的一个小软件:DB ...

  10. C - The C Answer (2nd Edition) - Exercise 1-1

    /* Run the "hello, world" program on your system. Experiment with leaving out parts of the ...