传送门

良心题解

#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 奶牛的邻居(好题)的更多相关文章

  1. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...

  2. [BZOJ1604] [Usaco2008 Open] Cow Neighborhoods 奶牛的邻居 (queue & set)

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...

  3. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 (Treap+单调队列)

    题面 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi( ...

  4. 【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000) ...

  5. BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    题目 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Time Limit: 5 Sec  Memory Limit: 64 MB Description ...

  6. bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...

  7. 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1604 这题太神了... 简直就是 神思想+神做法+神stl.. 被stl整的我想cry...首先,, ...

  8. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...

  9. BZOJ1604 & 洛谷2906:[USACO2008 OPEN]Cow Neighborhoods 奶牛的邻居——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1604 https://www.luogu.org/problemnew/show/P2906#sub ...

随机推荐

  1. MediaRecord一些使用记录

    今天学习了MediaRecord的使用,第一次使用做个记录. MediaRecord作用是声音录制,使用步骤如下: 1.新建出音频文件代码如下: 先创建出用于存储音频文件 File dir = new ...

  2. 联想e431笔记本更改硬盘模式bios设置的详细教程

    用硬盘安装系统,就要进入bios,将硬盘改为第一启动项即可重装系统.不同品牌的电脑,它的bios设置方法也就不同.那么,联想e431笔记本要如何更改硬盘模式呢?今天U大侠小编就和大家分享联想e431笔 ...

  3. 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest C Sequence (打表)

    打个表找找规律,到24445的时候乘2以后产生了0出现循环. 一般地,判断循环节是否存在可以用Floyd判圈算法. #include<bits/stdc++.h> using namesp ...

  4. Mac app 破解之路

    6年之前一直做过一些内存挂,脚本挂.都是比较低级的技术. 这几年期间,断断续续利用业余时间学了一些汇编的知识,当时只是想着破解游戏. 所有的黑技术都是业余自学的,没有老师可以问,只能百度和自己领悟,比 ...

  5. ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061) : 第一次设置MySQL也适用

    [MySQL的安装环境]:windows7 64位 [MySQL的版本]:mysql-8.0.16-winx64 [错误描述]: ERROR 2003 (HY000): Can't connect t ...

  6. dubbo 多连接,多线程池.

    1. consumer 多连接 Dubbo protocol options: <dubbo:protocolname=“dubbo” port=“9090” server=“netty” cl ...

  7. Spring框架中的aop操作之二 通过配置文件实现增强

    aop表达式写法 配置文件代码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...

  8. CSS在线压缩

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. ccf_201712-2

    题目 问题描述 有n个小朋友围成一圈玩游戏,小朋友从1至n编号,2号小朋友坐在1号小朋友的顺时针方向,3号小朋友坐在2号小朋友的顺时针方向,……,1号小朋友坐在n号小朋友的顺时针方向. 游戏开始,从1 ...

  10. 【树形dp】vijos1144小胖守皇宫

    细节很精妙 描述 huyichen世子事件后,xuzhenyi成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状:某些宫殿间可以互相望见.大内保卫森严,三步一岗,五步 ...