bzoj1604
treap+并查集
我们能想到一个点和最近点对连接,用并查集维护,但是这个不仅不能求,而且还是不对的,于是就看了题解
把距离转为A(x-y,x+y),这样两点之间的距离就是max(x'-X',y'-Y'),那么就可以求了,我们按转换后的x排序,维护一个区间,最大的x和最小的x差不超过c,然后把y插进treap里,每次查找前驱后继,如果距离小于等于c就连接,最后扫一遍统计答案就行
曼哈顿和切比雪夫距离是可以互相转换的,x=x-y,y=x+y就行,切比雪夫距离转换为曼哈顿距离转换回去也可以
#include<bits/stdc++.h>
using namespace std;
const int N = , seed = , inf = ;
struct data {
int x, y;
data(int x = , int y = ) : x(x), y(y) {}
bool friend operator < (data A, data B) { return A.x < B.x; }
} a[N];
int n, ans, root;
long long c;
pair<int, int> pre, nxt;
int sum[N], fa[N];
struct Treap {
int cnt, P;
pair<int, int> key[N];
int size[N], child[N][], p[N], tot[N];
inline int rand() { P = P * seed + ; return abs(P); }
inline void update(int x) { size[x] = size[child[x][]] + size[child[x][]] + tot[x]; }
inline void rotate(int &x, int t)
{
int y = child[x][t];
child[x][t] = child[y][t ^ ];
child[y][t ^ ] = x;
update(x); update(y); x = y;
}
inline void insert(int &x, pair<int, int> o)
{
if(x == ) { p[x = ++cnt] = rand(); key[x] = o; tot[x] = ; }
else
{
if(key[x] == o) ++tot[x];
else { int t = o > key[x]; insert(child[x][t], o); if(p[child[x][t]] > p[x]) rotate(x, t); }
}
update(x);
}
inline void erase(int &x, pair<int, int> o)
{
if(key[x] == o)
{
if(child[x][] == && child[x][] == ) { --tot[x]; if(tot[x] == ) x = ; else update(x); }
else { int t = p[child[x][]] > p[child[x][]]; rotate(x, t); erase(child[x][t ^ ], o); }
}
else erase(child[x][o > key[x]], o);
update(x);
}
inline void query_pre(int x, pair<int, int> o)
{
if(x == ) return;
if(key[x] > o) query_pre(child[x][], o);
else { if(key[x] > pre) pre = key[x]; query_pre(child[x][], o); }
}
inline void query_nxt(int x, pair<int, int> o)
{
if(x == ) return;
if(key[x] < o) query_nxt(child[x][], o);
else { if(key[x] < nxt) nxt = key[x]; query_nxt(child[x][], o); }
}
} treap;
inline int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
inline void connect(int x, int y)
{
int u = find(x), v = find(y);
if(u == v) return;
--ans;
fa[v] = u;
}
int main()
{
// freopen("nabor.in", "r", stdin);
// freopen("nabor.out", "w", stdout);
scanf("%d%lld", &n, &c);
ans = n;
for(int i = ; i <= n; ++i)
{
int x, y;
scanf("%d%d", &x, &y);
a[i] = data(x - y, x + y);
}
a[].x = inf;
sort(a + , a + n + );
treap.insert(root, {-inf, -});
treap.insert(root, {inf, -});
for(int i = ; i <= n; ++i) fa[i] = i;
int l = ;
for(int i = ; i <= n; ++i)
{
pair<int, int> o;
while(l <= i && a[i].x - a[l].x > c)
{
o = make_pair(a[l].y, l);
treap.erase(root, o);
++l;
}
nxt = {inf + , -};
pre = {-inf - , -};
o = make_pair(a[i].y, i);
treap.query_pre(root, o);
treap.query_nxt(root, o);
long long dis_pre, dis_nxt;
treap.insert(root, o);
dis_pre = (long long)a[i].y - (long long)pre.first;
dis_nxt = (long long)nxt.first - (long long)a[i].y;
if(dis_pre <= c && pre.second != -) connect(i, pre.second);
if(dis_nxt <= c && nxt.second != -) connect(i, nxt.second);
}
printf("%d ", ans);
ans = ;
for(int i = ; i <= n; ++i) ++sum[find(i)];
for(int i = ; i <= n; ++i) ans = max(ans, sum[i]);
printf("%d\n", ans);
// fclose(stdin);
// fclose(stdout);
return ;
}
bzoj1604的更多相关文章
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...
- 【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000) ...
- [BZOJ1604] [Usaco2008 Open] Cow Neighborhoods 奶牛的邻居 (queue & set)
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...
- bzoj1604 / P2906 [USACO08OPEN]牛的街区Cow Neighborhoods
P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 考虑维护曼哈顿距离:$\left | x_{1}-x_{2} \right |+\left | y_{1}-y_{2} ...
- BZOJ1604 & 洛谷2906:[USACO2008 OPEN]Cow Neighborhoods 奶牛的邻居——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1604 https://www.luogu.org/problemnew/show/P2906#sub ...
- 【bzoj1604】【[Usaco2008 Open]Cow Neighborhoods】简单的谈谈曼哈顿距离
(最近p站上不去要死了) Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃 ...
- 【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 旋转坐标系+并查集+Treap/STL-set
题目描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤ ...
- [BZOJ1604] [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(好题)
传送门 良心题解 #include <set> #include <cstdio> #include <iostream> #include <algorit ...
- bzoj1604 牛的邻居 STL
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...
随机推荐
- jquery 点击弹框
<a href="#" class="big-link" data-reveal-id="myModal" data-animatio ...
- 【05】AJAX实例-检测用户名是否存在(实例)
AJAX实例-检测用户名是否存在 用户注册时,需要填写个人信息,其中包括用户名.当用户输入完成时,JavaScript 需要及时检测用户名是否存在,如果存在给出提示,请用户更换用户名. 当然,这个 ...
- xtu summer individual-4 A - Beautiful IP Addresses
Beautiful IP Addresses Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on Code ...
- nyoj 96 n-1位数(处理前导 0 的情况)(string)
n-1位数 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则 ...
- 【(待重做)树状数组+dp+离散化】Counting Sequences
https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/G [题意] 给定一个数组a,问这个数组有多少个子序列,满足子序列中任意两个相邻数 ...
- hdu 3691最小割将一个图分成两部分
转载地址:http://blog.csdn.net/xdu_truth/article/details/8104721 题意:题给出一个无向图和一个源点,让你求从这个点出发到某个点最大流的最小值.由最 ...
- SpringBoot Data JPA 关联表查询的方法
SpringBoot Data JPA实现 一对多.多对一关联表查询 开发环境 IDEA 2017.1 Java1.8 SpringBoot 2.0 MySQL 5.X 功能需求 通过关联关系查询商店 ...
- Redis事务【十二】
一.概述: 和众多其它数据库一样,Redis作为NoSQL数据库也同样提供了事务机制.在Redis中,MULTI/EXEC/DISCARD/WATCH这四个命令是我们实现事务的基石.相信对有关系型数据 ...
- POJ—— 2117 Electricity
Electricity Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5620 Accepted: 1838 Descr ...
- 高数(A)下 第十二章
12.1 12.2 12.3 12.4 12.5 12.6 自测题