题目链接: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 扩展replaceAll

    //扩展replaceAll; String.prototype.replaceAll = function(s1,s2) { return this.replace(new RegExp(s1,&q ...

  2. mybatis技术文章

    http://legend2011.blog.51cto.com/3018495/1600478

  3. C#比較对象的相等性

    对于相等的机制全部不同,这取决于比較的是引用类型还是值类型.以下分别介绍引用类型和值类型的相等性. 1.比較引用类型的相等性 System.Object定义了三种不同的方法,来比較对象的相等性:Ref ...

  4. Java使用笔记之stream和sorted使用

    //对象类型stream排序List<User> users = new ArrayList<User>(){ { add(new User("a", &q ...

  5. vim 查找与替换

    一.vim 查找 1. 正向查找 / 与 反向查找 ? 2. 退出查找 <Esc> 3. 跳转到下一处匹配 n ,跳转到上一处匹配 N 4. /<CR> 正向跳转到相同模式的下 ...

  6. XXE攻击

    1.背景 现在很多应用都存在XXE(XML External Entity attack)漏洞,就是xml外部实体攻击,比如facebook,很多XML的解析器默认是含有XXE漏洞的. 2.xml的定 ...

  7. null的比较问题

    select count(*) from table_a WHERE  status=1 and end_time<now(); 写这个sql的时候有点纠结,万一end_time是null怎么办 ...

  8. js关闭浏览器事件,js关闭浏览器提示及相关函数

    关于浏览器关闭事件的相关描述 有些朋友想在浏览器关闭的时候,弹出alert .confirm或者prompt等.实验证明,这种做法是失败的,原因是浏览器关闭事件自动屏蔽执行js的某些方法,从而防止恶意 ...

  9. 37、pendingIntent 点击通知栏进入页面

    转载: http://blog.csdn.net/yuzhiboyi/article/details/8484771 https://my.oschina.net/youranhongcha/blog ...

  10. memcached 搭建

     linux 下memcached 的搭建 memcached 下载地址:http://www.danga.com/memcached/dist/ libevent 下载地址:http://libev ...