[BZOJ1604] [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(好题)
#include <set>
#include <cstdio>
#include <iostream>
#include <algorithm>
#define N 100001
#define LL long long
#define INF (~(1 << 31)) using namespace std; int n, c, h = 1, ans, ans1;
int f[N], sum[N]; struct node
{
LL x, y;
node(int x = 0, int y = 0) : x(x), y(y) {}
}p[N]; multiset <node> s;
multiset <node> :: iterator it; inline bool operator < (const node &a, const node &b)
{
return a.x < b.x;
} inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline int find(int x)
{
return x == f[x] ? x : f[x] = find(f[x]);
} inline int merge(int x, int y)
{
x = find(x);
y = find(y);
if(x ^ y)
{
f[x] = y;
ans--;
}
} int main()
{
int i;
LL x, y;
ans = n = read();
c = read();
for(i = 1; i <= n; i++) f[i] = i;
for(i = 1; i <= n; i++)
{
x = read();
y = read();
p[i].x = x + y;
p[i].y = x - y;
}
sort(p + 1, p + n + 1);
s.insert(node(-INF, 0));
s.insert(node(INF, 0));
for(i = 1; i <= n; i++)
{
while(p[i].x - p[h].x > c)
{
s.erase(s.lower_bound(node(p[h].y, h)));
++h;
}
y = p[i].y;
it = s.lower_bound(node(p[i].y, i));
node r = *it, l = *--it;
if(r.x - y <= c) merge(r.y, i);
if(y - l.x <= c) merge(l.y, i);
s.insert(node(p[i].y, i));
}
for(i = 1; i <= n; i++) sum[find(i)]++;
for(i = 1; i <= n; i++) ans1 = max(ans1, sum[i]);
printf("%d %d\n", ans, ans1);
return 0;
}
[BZOJ1604] [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(好题)的更多相关文章
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...
- [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 奶牛的邻居 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 ...
随机推荐
- 【读书笔记】构建之法(CH4~CH6)
从chapter4至chapter6,围绕着构建过程的合作讨论构建之法,而合作与个人工作的区别却以一个微妙的问题为开端:阅读别人的代码有多难? 两人合作:(驾驶员与领航员) 合作要注意代码风格规范与设 ...
- css水平垂直居中的几个方法和技巧/居中之美
水平居中设置-行内元素 我们在实际工作中常会遇到需要设置水平居中场景,今天我们就来看看怎么设置水平居中的. 如果被设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置 text-ali ...
- SQL server 数据库基础语句 子查询 基础函数
上一章 说了下 子查询的意义是 把一条查询语句当做值来使用 select *from car //查询汽车的信息 假设我知道一个汽车的编号是 c021 但是我要查询 比这个汽车价格高的汽车信息 ...
- tomcat+nginx 横向扩展
1.分别在电脑上部署两个tomcat tomcat1 tomcat2 2.不是nginx 并启动 输入 localhost 并进入nginx欢迎界面,证明部署成功 3.修改nginx 配置 ngin ...
- Fixed table
废话不多说,直接代码. <!DOCTYPE> <html> <head> <meta charset="utf-8"/> <s ...
- js 获取当前URL信息
document.location 这个对象包含了当前URL的信息 location.host 获取port号 location.hostname 设置或获取主机名称 location.href 设置 ...
- sql mybatis 使用concat乱码
先贴代码,这是sql查询里面 select id,name,sex,phone,present,adder, CONCAT("从业",experience,"年" ...
- Webpack的使用指南-Webpack小结
参考文章: https://baijiahao.baidu.com/s?id=1594972657801970108&wfr=spider&for=pc 使用Webpack有一段时间了 ...
- BestCoder Round#15 1001-Love
http://acm.hdu.edu.cn/showproblem.php?pid=5082 Love Time Limit: 2000/1000 MS (Java/Others) Memory ...
- scanf函数详解
函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]);scanf()函数是通用终端格式化输入函数,它从标准输入设备(键 ...