[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 ...
随机推荐
- python-网络编程-socket编程
一.TCP\IP(网络通信协议)简介 TCP:(Transmission Control Protocol)传输控制协议,面向有连接的通信协议 UDP:数据报文协议,面向无连接的通信协议 ...
- C# ASP.NET 优化程序性能、降低内存使用、提高程序运行速度
首先纪念一下今天的股票大跌抓个图,虽然我自己损失不是很大,但是应该大多人都损失不小.也可能有人会继续跳楼,也可能是股市一个新的转折点来了. 接着还是重点关注自己写代码优化的主题吧.软件系统当访问量不大 ...
- 从一个简单的ASP.NET 5站点开启.NET跨平台之旅
在经历了阿里云上“黑色1秒”的空欢喜之后,我们“被迫”考虑实现.NET的跨平台,将Web服务器由Windows换成Linux.而这种“被迫”在一个存在已久的愿望下,变得水到渠成.这个愿望就是 —— “ ...
- mysql Workbench 执行删除命令
SET SQL_SAFE_UPDATES = 0;delete from table1; SET SQL_SAFE_UPDATES = 1;
- 从头写个http client(java)
不熟悉java,但我熟悉http,然后从头打造个简单的httpclient,支持get/post,支持gzip,支持重定向,支持encoding,支持transfer-encoding,支持ssl,支 ...
- Node+Socketio实现消息群发功能
注:本博文是作者原创,转载请注明出处. 在项目中时常会使用到socketio,今天我们就来实现Node+socketio实现群发消息功能, 项目源码:https://github.com/zhangx ...
- 1125MySQL Sending data导致查询很慢的问题详细分析
-- 问题1 tablename使用主键索引反而比idx_ref_id慢的原因EXPLAIN SELECT SQL_NO_CACHE COUNT(id) FROM dbname.tbname FORC ...
- 正确遍历ElasticSearch索引
1:ElasticSearch的查询过程 2:由ES查询模式引起的深度分页问题 3:如何正确遍历索引中的数据 ElasticSearch的查询过程 es的数据查询分两步: 第一步是的结果是获取满足查询 ...
- hbase shell 常见命令
quick start from official Hbase hbase(main):003:0> create 'test', 'cf' 0 row(s) in 1.2200 second ...
- Xcode真机测试could not find developer disk image解决方法
原文地址:http://my.oschina.net/u/2340880/blog/521700 Xcode真机测试could not find developer disk image解决方法 在使 ...