[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 ...
随机推荐
- React Native 手工搭建环境 之iOS篇
常识 React native 开发服务器 在开发时,我们的框架是这样的:  当正式发布进入到生产环境时,开发服务器上所有的js文件将会被编译成包的形式,直接嵌入到客户端内.这时,已经不再需要开发服 ...
- iOS Block的本质(四)
iOS Block的本质(四) 上一篇文章iOS Block的本质(三)中已经介绍过block变量的捕获,本文继续探寻block的本质. 1. block内修改变量的值 int main(int ar ...
- SQL语句,mysql数据库
sql语句,把一张表里的数据,和特定数据(固定常量)新插入另一张表 ,,, from wm_jobpoint INSERT INTO wm_department(departmentcode,depa ...
- Grace Huang 2017/1/12
原文 Huang doesn't think of acting as pretending to be someone else.Rather,she considers it an opportu ...
- gunzip
gunzip——解压缩.gz的压缩文件 GNU unzip 命令所在路径:/bin/gunzip 示例1: # gunzip services.gz 解压缩当前目录下的services.gz文件,执行 ...
- eclipse中增加matplotlib、web应用’和pip框架包
由于python主要应用在Linux下和相关的vc下,对于熟悉eclipse的我来说,这是一个难题,通过在命令行中转pip可以安装python任何信息,具体的插件直接在一下网页中搜索https://p ...
- Resize a UIImage the right way
When deadlines loom, even skilled and experienced programmers can get a little sloppy. The pressure ...
- WPF中在后台实现控件样式
加入现在有一个Button的样式如下: <Style TargetType="{x:Type Button}" x:Key="MyButton">. ...
- urllib基础-请求对象request
简单的案例-爬取百度首页 from urllib import request ''' 爬取百度首页 ''' # 确定爬去目标 base_url = 'http://www.baidu.com' # ...
- JS实现跑马灯效果(向左,向上)
<html> <head> <title>JS实现跑马灯效果</title> <style> * { font-size:12px; fon ...