P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

题目描述

Those Who Know About Cows are aware of the way cows group into 'Cow Neighborhoods'. They have observed Farmer John's N (1 <= N <= 100,000) cows (conveniently numbered 1..N) as they graze, each at her own unique integer rectilinear coordinate, on a pasture whose X and Y coordinates are in the range 1..1,000,000,000.

Two cows are neighbors if at least one of two criteria is met:

  1. If the cows are no further than some integer Manhattan distance C (1 <= C <= 1,000,000,000) apart, they are neighbors. [Manhattan distance is calculated as d = |x1-x2| + |y1-y2|.] 2) If cow A is a neighbor of cow Z and cow B is a neighbor of cow Z, then cow A is a neighbor of cow B (the 'transitive closure of neighbors').

Given the locations of the cows and the distance C, determine the the number of neighborhoods and the number of cows in the largest neighborhood.

By way of example, consider the pasture below. When C = 4, this pasture has four neighborhoods: a big one on the left, two neighborhoods of size 1 (the lonesome cows), and a huge neighborhood on the right with 60 different cows.

.....................................*.................
....*...*..*.......................***.................
......*...........................****.................
..*....*..*.......................*...*.******.*.*.....
........................*.............***...***...*....
*..*..*...*..........................*..*...*..*...*...
.....................................*..*...*..*.......
.....................................*..*...*..*.......
...*................*..................................
.*..*............................*.*.*.*.*.*.*.*.*.*.*.
.*.....*..........................*.*.*.*.*.*.*.*.*.*.*
....*..................................................

The input file describes cow locations by integer X,Y coordinates, where the lower left corner is (1,1) and cows close to that corner appear at both (2,2) and (5,1) in the example above.

For a given pasture, determine both the number of cow neighborhoods and the number of cows resident in the largest cow neighborhood.

The above picture is sample test case 2, which will be evaluated for you upon submission.

Partial feedback for some test cases will be provided on the first 10 submissions.

Time Limit: 2 seconds

Memory Limit: 32MB

了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标\(X_i\),\(Y_i\)(\(1\leq X_i,Y_i\leq [1 \cdots 10^9]\);\(X_i,Y_i \in \text{整数}\).当满足下列两个条件之一,两只奶牛i和j是属于同一个群的:

  1. 两只奶牛的曼哈顿距离不超过\(C(1\leq C\leq 10^9)\),即\(|X_i - x_i|+|Y_i - y_i|\leq C\).

  2. 两只奶牛有共同的邻居.即,存在一只奶牛\(k\),使\(i\)与\(k\),\(j\)与\(k\)均同属一个群.

给出奶牛们的位置,请计算草原上有多少个牛群,以及最大的牛群里有多少奶牛

输入输出格式

输入格式:

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Line i+1 describes cow i's location with two

space-separated integers: \(X_i\) and \(Y_i\)

输出格式:

* Line 1: A single line with a two space-separated integers: the number of cow neighborhoods and the size of the largest cow neighborhood.

输入输出样例

输入样例#1:

4 2
1 1
3 3
2 2
10 10

输出样例#1:

2 3

说明

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_i\)与\(x_i\)表示的意义是不同的

我们观察两个点\(i\)、\(j\)的曼哈顿距离\(|X_i-X_j|+|Y_i-Y_j|\)。

  • 如果\(X_i-X_j\)与\(Y_i-Y_j\)同号,曼哈顿距离为\(|X_i+Y_i-(X_j+Y_j)|\),且\(|X_i-Y_i-(X_j-Y_j)|\)偏小

  • 如果异号,曼哈顿距离为\(|X_i-Y_i-(X_j-Y_j)|\),且\(|X_i+Y_i-(X_j+Y_j)|\)偏小

因此,我们发现,如果设\(x_i=X_i+Y_i\),\(y_i=X_i-Y_i\),\(i\)、\(j\)之间的曼哈顿距离即为\(\max(|x_i-x_j|,|y_i-y_j|)\)

然后用平衡树multiset一通乱搞就可以啦!

我们按照\(x_i\)为第一关键字,\(y_i\)为第二关键字排序,依次处理每个点。设当前处理的点为\(i\)。我们用并查集维护一个个群。

首先把所有\(x\)值小于\(x_i-C\)的点全部\(erase\)(删除)掉。

寻找第一个大于等于\(y_i\)的点\(j\)(lower_bound)。如果\(y_j-y_i\le C\),很明显,\(j\)与\(i\)是同一个群的。

但是如果还有大于\(y_i\)的点\(k\)也满足条件呢?很明显,如果\(y_k-y_i\le C\),那么\(y_k-y_j\le y_k-y_i\le C\),在处理\(j\)或\(k\)的时候已经将它们合并(这取决于\(x\)的大小),不必再管。

然后再康康小于\(y_i\)的点有没有符合条件的就好啦!

代码

#include<bits/stdc++.h>
using namespace std;
#define IT multiset<pi>::iterator
#define MAXN 100005
#define LL long long
#define pi pair<LL, int> struct node{
int x, y;
bool operator < ( const node &t )const{//重载运算符
if ( x == t.x ) return y < t.y;
return x < t.x;
}
void input(){
scanf( "%d%d", &x, &y );
x = x + y, y = x - y - y;//读入并把X、Y转换成x、y
}
}a[MAXN]; multiset<pi> s;//因为要合并,还得记录编号(排序后的)。。所以用了个pair
IT p[MAXN];//迭代器数组,用于删除x过小的元素 int f[MAXN], sm[MAXN];//并查集、记录每个群的牛数(最后再处理 int find( int x ){ return x == f[x] ? x : ( f[x] = find(f[x]) ); }
void Merge( int x, int y ){
x = find(x); y = find(y);
f[x] = y;
} int N, C, x, y; int main(){
scanf( "%d%d", &N, &C );
for ( int i = 1; i <= N; ++i ) a[i].input(), f[i] = i;
sort( a + 1, a + N + 1 );
s.insert( make_pair( 1ll << 60, -1 ) ); s.insert( make_pair( -( 1ll << 60 ), -1 ) );//避免边界问题
p[1] = s.insert(make_pair( a[1].y, 1 )); int tmp(1);//第一个点直接插入即可。 for ( int i = 2; i <= N; ++i ){
while( a[i].x - a[tmp].x > C ) s.erase(p[tmp++]);//把不满足要求的点删除 IT t(s.lower_bound(make_pair( a[i].y, -1 )));//找第一个大于等于y的
if ( t->first - a[i].y <= C ) Merge( i, t->second );//满足要求,合并
t--;//找第一个小于y的
if ( a[i].y - t->first <= C ) Merge( i, t->second );//满足要求,合并 p[i] = s.insert( make_pair( a[i].y, i ) );//插入点并记录迭代器
}
int ans1(0), ans2(0);
for ( int i = 1; i <= N; ++i ){
if ( find(i) == i ) ans1++;
ans2 = max( ans2, ++sm[find(i)] );
}
printf( "%d %d\n", ans1, ans2 );
return 0;
}

「洛谷P2906」[USACO08OPEN]牛的街区Cow Neighborhoods 解题报告的更多相关文章

  1. bzoj1604 / P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

    P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 考虑维护曼哈顿距离:$\left | x_{1}-x_{2} \right |+\left | y_{1}-y_{2} ...

  2. 洛谷 P2906 [USACO08OPEN]牛的街区Cow Neighborhoods | Set+并查集

    题目: https://www.luogu.org/problemnew/show/P2906 题解: 垃圾水题 #include<cstdio> #include<algorith ...

  3. P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

    传送门 曼哈顿距离好像不好直接算,我们可以把牛的坐标转化一下以方便计算距离 (x,y) --> (x+y,x-y) 那么距离就可以表示成 $max(\left |x_1-x_2  \right ...

  4. [USACO08OPEN]牛的街区Cow Neighborhoods

    题目描述: luogu 题解: 技巧题. 曼哈顿距离:$|x1-x2|+|y1-y2|$ 切比雪夫距离:$\max(|x1-x2|,|y1-y2|)$ 曼哈顿距离转切比雪夫距离:$(x,y)-> ...

  5. 「区间DP」「洛谷P1043」数字游戏

    「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...

  6. 洛谷P1522 [USACO2.4]牛的旅行 Cow Tours

    洛谷P1522 [USACO2.4]牛的旅行 Cow Tours 题意: 给出一些牧区的坐标,以及一个用邻接矩阵表示的牧区之间图.如果两个牧区之间有路存在那么这条路的长度就是两个牧区之间的欧几里得距离 ...

  7. 「洛谷4197」「BZOJ3545」peak【线段树合并】

    题目链接 [洛谷] [BZOJ]没有权限号嘤嘤嘤.题号:3545 题解 窝不会克鲁斯卡尔重构树怎么办??? 可以离线乱搞. 我们将所有的操作全都存下来. 为了解决小于等于\(x\)的操作,那么我们按照 ...

  8. 「洛谷3338」「ZJOI2014」力【FFT】

    题目链接 [BZOJ] [洛谷] 题解 首先我们需要对这个式子进行化简,否则对着这么大一坨东西只能暴力... \[F_i=\sum_{j<i} \frac{q_iq_j}{(i-j)^2}-\s ...

  9. 「BZOJ2733」「洛谷3224」「HNOI2012」永无乡【线段树合并】

    题目链接 [洛谷] 题解 很明显是要用线段树合并的. 对于当前的每一个连通块都建立一个权值线段树. 权值线段树处理操作中的\(k\)大的问题. 如果需要合并,那么就线段树暴力合并,时间复杂度是\(nl ...

随机推荐

  1. php parse_url linux 解析问题

    耕毅 解析url函数parse_url() (PHP 4, PHP 5, PHP 7) parse_url — 解析 URL,返回其组成部分 mixed parse_url ( string $url ...

  2. Android教程 -06 Activity的生命周期

    本篇播客我们重点介绍下如下知识点: 程序启动原理 Activity生命周期 Activity销毁与创建 视频建议采用超清模式观看, 欢迎点击订阅我的优酷 程序启动原理 当用户从主界面点击程序图标时,系 ...

  3. [转]Java多线程学习(总结很详细!!!)

    Java多线程学习(总结很详细!!!) 此文只能说是java多线程的一个入门,其实Java里头线程完全可以写一本书了,但是如果最基本的你都学掌握好,又怎么能更上一个台阶呢? 本文主要讲java中多线程 ...

  4. mybatis 嵌套查询与懒加载

    懒加载:对于页面有很多静态资源的情况下(比如网商购物页面),为了节省用户流量和提高页面性能,可以在用户浏览到当前资源的时候,再对资源进行请求和加载. fetchType="lazy" ...

  5. 2018-8-10-win10-uwp-获得元素绝对坐标

    title author date CreateTime categories win10 uwp 获得元素绝对坐标 lindexi 2018-08-10 19:16:51 +0800 2018-2- ...

  6. servicemix-3.2.1 内置的服务引擎和绑定组件

    服务引擎: servicemix-bean servicemix-camel servicemix-cxf-se servicemix-drools servicemix-eip servicemix ...

  7. MySQL 数据库中如何把A表的数据插入到B表?

    web开发中,我们经常需要将一个表的数据插入到另外一个表,有时还需要指定导入字段,设置只需要导入目标表中不存在的记录,虽然这些都可以在程序中拆分成简单sql来实现,但是用一个sql的话,会节省大量代码 ...

  8. 4-4 Selector有一个方法可以获取Selector中的文本内容---extract()

    ####### 例如: response.xpath('//div[@class ="entry-header"]/h1/text()').extract()

  9. java多异常处理

    声明异常时尽可能声明具体异常类型,方便更好的处理; 方法声明几个异常就对应有几个catch块; 若多个catch块中的异常出现继承关系,父类异常catch块放在最后; 在catch语句块使用Excep ...

  10. P1083 合并序列

    题目描述 有N个单词和字符串T,按字典序输出以字符串T为前缀的所有单词. 输入格式 输入文件第一行包含一个正整数N: 接下来N行,每行一个单词,长度不超过100: 最后一行包含字符串T. 已知:1≤N ...