[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 ...
随机推荐
- 第7章 权限管理(3)_文件系统属性和sudo权限
3. 文件系统属性chattr权限 (1)chattr命令 命令格式: #chattr [+-=][选项] 文件或目录名 +.-.= 分别表示增加权限.删除权限和赋于某种权限 选项 i:主要用来防止对 ...
- MySQL的基本知识 -- 命令
1.数据库和表 SHOW DATABASES; 返回可用数据库的一个列表 SHOW TABLES; 返回一个数据库内的表的列表 SHOW COLUMNS FROM tableName; 返回数据表的表 ...
- jQuery中slice()用法总结
<!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8&quo ...
- 一份关于Swift语言学习资源的整理文件
一份关于Swift语言学习资源的整理文件 周银辉 在这里下载 https://github.com/ipader/SwiftGuide
- RecyclerView的使用(四)
前面我们已经实现了RecyclerView的大部分功能了,但是有个很明显的缺陷-------没有点击效果!这就坑爹了 ListView自带点击效果好嘛!连这个都要自己定义.... 话不多说,下面就来为 ...
- C#属性-索引器-里氏替换-多态-虚方法-抽象-接口-泛型-
1.属性 //属性的2种写法 public class person { private string _name; public string Name { get { return _name; ...
- I finally made sense of front end build tools. You can, too.
来源于:https://medium.freecodecamp.com/making-sense-of-front-end-build-tools-3a1b3a87043b#.nvnd2vsd8 ...
- sharedUserId
android:sharedUserId sharedUserId的作用是让两个应用程序共享一个user id,我们都知道linux进程给每一个应用程序分配了一个独立的user id,所以如果两个或多 ...
- float,double和decimal类型
float:浮点型,含字节数为4,32bit,数值范围为-3.4E38~3.4E38(7个有效位) double:双精度实型,含字节数为8,64bit数值范围-1.7E308~1.7E308(15个有 ...
- 了解了下 Google 最新的 Fuchsia OS
就是看到篇报道,有点好奇,就去FQ挖了点东西回来. 我似乎已开始就抓到了重点,没错,就是 LK . LK 是 Travis Geiselbrecht 写的一个针对 ARM 的嵌入式操作系统,开源的.点 ...