[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
试题描述
输入
第1行输入N和C,之后N行每行输入一只奶牛的坐标.
输出
仅一行,先输出牛群数,再输出最大牛群里的牛数,用空格隔开.
输入示例
输出示例
数据规模及约定
见“试题描述”
题解
把曼哈顿距离转换成“切比雪夫”距离,即每个点 (x, y) 变成 (x+y, x-y),那么两点 (x1, y1) (x2, y2) 间曼哈顿距离 = max{ |x1 - x2|, |y1 - y2| },于是把新点按 x 排序,滑动窗口控制成 c 的宽度,然后平衡树维护 y 这一维,每次找到一个点的前驱、后继如果 y 坐标相差小于等于 c 就在并查集中合并一下。最后随便统计统计。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 100010
#define oo 2147483647
struct Node {
int v, r, id;
Node() {}
Node(int _1, int _2, int _3): v(_1), r(_2), id(_3) {}
bool operator < (const Node& t) const { return v != t.v ? v < t.v : id < t.id; }
bool operator == (const Node& t) const { return v == t.v && id == t.id; }
} ns[maxn];
int rt, ToT, ch[2][maxn], fa[maxn];
void rotate(int u) {
int y = fa[u], z = fa[y], l = 0, r = 1;
if(z) ch[ch[1][z]==y][z] = u;
if(ch[1][y] == u) swap(l, r);
fa[u] = z; fa[y] = u; fa[ch[r][u]] = y;
ch[l][y] = ch[r][u]; ch[r][u] = y;
return ;
}
void insert(int& o, int v, int id) {
if(!o) {
ns[o = ++ToT] = Node(v, rand(), id);
return ;
}
bool d = ns[o] < Node(v, -1, id);
insert(ch[d][o], v, id); fa[ch[d][o]] = o;
if(ns[ch[d][o]].r > ns[o].r) {
int t = ch[d][o];
rotate(t); o = t;
}
return ;
}
void del(int& o, int v, int id) {
if(!o) return ;
if(ns[o] == Node(v, -1, id)) {
if(!ch[0][o] && !ch[1][o]) o = 0;
else if(!ch[0][o]) {
int t = ch[1][o]; fa[t] = fa[o]; o = t;
}
else if(!ch[1][o]) {
int t = ch[0][o]; fa[t] = fa[o]; o = t;
}
else {
bool d = ns[ch[1][o]].r > ns[ch[0][o]].r;
int t = ch[d][o]; rotate(t); o = t;
del(ch[d^1][o], v, id);
}
}
else {
bool d = ns[o] < Node(v, -1, id);
del(ch[d][o], v, id);
}
return ;
}
Node Findlow(int o, int v, int id) {
Node err(-oo, 233, -1), que(v, -1, id);
if(!o) return err;
bool d = ns[o] < que;
return max(ns[o] < que ? ns[o] : err, Findlow(ch[d][o], v, id));
}
Node Findupp(int o, int v, int id) {
Node err(oo, 233, -1), que(v, -1, id);
if(!o) return err;
bool d = ns[o] < que;
return min(que < ns[o] ? ns[o] : err, Findupp(ch[d][o], v, id));
} struct Point {
int x, y;
Point() {}
Point(int _, int __): x(_), y(__) {}
bool operator < (const Point& t) const { return x != t.x ? x < t.x : y < t.y; }
} ps[maxn]; int pa[maxn], siz[maxn];
int findset(int x) { return x == pa[x] ? x : pa[x] = findset(pa[x]); } int main() {
int n = read(), c = read();
for(int i = 1; i <= n; i++) {
int x = read(), y = read();
ps[i] = Point(x + y, x - y);
} sort(ps + 1, ps + n + 1);
int cnt = n, mxs = 1;
for(int i = 1; i <= n; i++) pa[i] = i, siz[i] = 1;
int l = 1, r = 1;
for(; r <= n; r++) {
while(l <= r && ps[r].x - ps[l].x > c) del(rt, ps[l].y, l), l++;
Node tmp = Findlow(rt, ps[r].y, r);
// printf("tmp: %d %d\n", tmp.id, tmp.v);
if(tmp.v != -oo && ps[r].y - tmp.v <= c) {
int u = findset(r), v = findset(tmp.id);
if(u != v) siz[u] += siz[v], mxs = max(mxs, siz[u]), pa[v] = u, cnt--;
}
tmp = Findupp(rt, ps[r].y, r);
// printf("tmp: %d %d\n", tmp.id, tmp.v);
if(tmp.v != oo && tmp.v - ps[r].y <= c) {
int u = findset(r), v = findset(tmp.id);
if(u != v) siz[u] += siz[v], mxs = max(mxs, siz[u]), pa[v] = u, cnt--;
}
insert(rt, ps[r].y, r);
} printf("%d %d\n", cnt, mxs); return 0;
}
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居的更多相关文章
- [BZOJ1604] [Usaco2008 Open] Cow Neighborhoods 奶牛的邻居 (queue & set)
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 (Treap+单调队列)
题面 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi( ...
- [BZOJ1604] [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(好题)
传送门 良心题解 #include <set> #include <cstdio> #include <iostream> #include <algorit ...
- 【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000) ...
- BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
题目 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Time Limit: 5 Sec Memory Limit: 64 MB Description ...
- bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...
- 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1604 这题太神了... 简直就是 神思想+神做法+神stl.. 被stl整的我想cry...首先,, ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...
- BZOJ1604 & 洛谷2906:[USACO2008 OPEN]Cow Neighborhoods 奶牛的邻居——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1604 https://www.luogu.org/problemnew/show/P2906#sub ...
随机推荐
- [No000076]用Eclipse编写python(配置PyDev插件)
下载,安装python解释器 地址:https://www.python.org/downloads/ 下载,安装java jdk 地址:http://www.oracle.com/technetwo ...
- Javascript函数式编程要掌握的知识点讲解
一:理解call和apply 及arguments.callee ECMAScript3给Function的原型定义了两个方法,他们是Function.prototype.call 和 Functio ...
- NSBundle
属性: .使用类方法创建一个NSBundler对象 + (NSBundle *)mainBundle; eg:[NSBundle mailBundle]; .使用路径获取一个NSBundle 对象,这 ...
- 使用axis2 soapmonitor监控soap数据
Using the SOAP Monitor SOAPMonitor使用 使用SOAPMonitor axis2开发笔记-消息监控 WebService大讲堂之Axis2(10):使用soapmoni ...
- jsoup开发网页客户端3
这个系列好久没更新,最近好忙,老大说未来是Html5的,所以最近一直学习前端以及Html5的一些东西.Android5.0的诞生,让我们眼前一亮,独特的Material风格更是吸引了无数人. 话说不学 ...
- nodejs+express+mysql 增删改查
之前,一直使用的是nodejs+thinkjs来完成自己所需的项目需求,而对于nodejs中另外一中应用框架express却了解的少之又少,这两天就简单的了解了一下如何使用express来做一些数据库 ...
- Webpack 中文指南
来源于:http://webpackdoc.com/index.html Webpack 是当下最热门的前端资源模块化管理和打包工具.它可以将许多松散的模块按照依赖和规则打包成符合生产环境部署的前端资 ...
- iOS地图 -- 定位中的CLLocation的介绍与小练习
通过定位练习,熟悉CLLocation 在上篇笔记中提到了CLLocation类,这里通过练习来讲解一下这个类,类中包含了获取到的用户位置的信息 coordinate --> 坐标,经度和纬度 ...
- 【BZOJ 3642】Phi的反函数
http://www.lydsy.com/JudgeOnline/problem.php?id=3643 因为\[\varphi(n)=\prod_i p_i^{k_i-1}(p_i-1),n=\pr ...
- 在Vi里面实现字符串的批量替换
在Vi里面实现字符串的批量替换. a. 文件内全部替换: %s#abc#def#g(用def替换文件中所有的abc) 例如把一个文本文件里面的"linuxidc.com"全部替换成 ...