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;
}

转载于:https://www.cnblogs.com/louhancheng/p/10293006.html

「洛谷P2906」[USACO08OPEN]牛的街区Cow Neighborhoods 解题报告相关推荐

  1. 信息学奥赛一本通 1343:【例4-2】牛的旅行 | 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours

    [题目链接] ybt 1343:[例4-2]牛的旅行 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours [题目考点] 1. 图论 最短路径 Floyd算法 Floyd算法时间复杂度 ...

  2. 洛谷【C++编程基础】递归函数初步 专题解题报告

    洛谷[C++编程基础]递归函数初步 专题解题报告 T1-T89304 递归求和 题目描述 用递归的方法求1+2+3+4+-+(n-1)+n的值. 输入格式 一个整数n.(1<=n<=100 ...

  3. 「洛谷P1343」地震逃生 解题报告

    P1343 地震逃生 题目描述 汶川地震发生时,四川XX中学正在上课,一看地震发生,老师们立刻带领x名学生逃跑,整个学校可以抽象地看成一个有向图,图中有n个点,m条边.1号点为教室,n号点为安全地带, ...

  4. 「洛谷2495」「BZOJ3052」「SDOI2001」消耗战【虚树+树形动态规划】

    题目大意 给你\(k\)个点,让这一些点和一号节点断开,删去某一些边,求最小的删去边权之和. 做题的心路历程 做了\(HG\)昨天的模拟赛,深深感觉到了窝的菜,所以为了\(A\)掉T1这一道毒瘤,窝就 ...

  5. 「洛谷P2397」 yyy loves Maths VI (mode) 解题报告

    P2397 yyy loves Maths VI (mode) 题目背景 自动上次redbag用加法好好的刁难过了yyy同学以后,yyy十分愤怒.他还击给了redbag一题,但是这题他惊讶的发现自己居 ...

  6. 「洛谷P3469」[POI2008]BLO-Blockade 解题报告

    P3469[POI2008]LO-Blockade 题意翻译 在Byteotia有n个城镇. 一些城镇之间由无向边连接. 在城镇外没有十字路口,尽管可能有桥,隧道或者高架公路(反正不考虑这些).每两个 ...

  7. 「洛谷 3768」简单的数学题

    传送门 problem 给定 nnn 和 ppp,求: ∑i=1n∑j=1nijgcd⁡(i,j)\sum_{i=1}^n\sum_{j=1}^nij\gcd(i,j)i=1∑n​j=1∑n​ijgc ...

  8. 「洛谷 P5043」:树同构【树哈希】

    P5043 [模板]树同构([BJOI2015]树的同构 题目描述 树是一种很常见的数据结构. 我们把NNN个点,N−1N−1N−1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点 ...

  9. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  10. 洛谷P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

最新文章

  1. 博客园markdown语法扩展
  2. C++ Prime:switch内部的变量定义
  3. find the nth digit(数学 思维)
  4. [Java基础]增强for循环
  5. codeforces 939C Convenient For Everybody 简直羞耻
  6. 推荐Linux内核书籍
  7. div+css布局注意点
  8. 【SpringBoot】Spring boot 测试类 找到不到MySQL 驱动
  9. 电大本科c及语言设计形考答案,2020国家开放大学电大《C语言程序设计》网络课形考任务4作业及答案(15页)-原创力文档...
  10. Java笔记(二十一) 动态代理
  11. Graphics.DrawLine 方法
  12. 服务器硬盘容量为0,硬盘容量不一样 raid0 扩容也可以很自如?
  13. BLE_BQB Test_Modulation Characteristics, LE Coded (S=8)_RF-PHY/TRM/BV-13-C
  14. 复合类型(json)
  15. 百度AI平台申请使用流程
  16. 疯狂java  进行回顾
  17. 利用DirectShow开发C#版的音频文件播放器(补充完善)
  18. Office 2016 系列 VOL版本下载
  19. yum 代理(系统代理对 yum 不生效)
  20. 《位置大数据隐私管理》—— 1.3 LBS中的个人隐私与挑战

热门文章

  1. CSS盒子模型、浮动+例子分析
  2. Hierachy Viewer 使用 monitor命令
  3. python对电商运营有帮助吗_做电商运营需要学习python嘛?
  4. Outlook2016邮箱配置说明文档
  5. 多国语言点阵字库合并!!!
  6. Android学习别“走弯路”,移动端混合开发框架
  7. 梯度、散度、旋度的关系
  8. javaweb_表单设计
  9. 基于Arduino的多功能数字时钟
  10. 开启电脑卓越性能模式