[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 ...
随机推荐
- 转:android 屏幕适配小结
做android开发,开源嘛,满市场都是凌乱的机型,总少不了适配这样或那样的型号.在这里分享一下自己在开发中用到的方法. 首先要介绍一下drawable-mdpi.drawable-hdpi-1280 ...
- LR中webservice服务测试的脚本
Action(){ /* 测试QQ是否在线的功能接口 输入参数:QQ号码 String,默认QQ号码:8698053. 返回数据:String,Y = 在线:N = 离线:E = QQ号码错误:A = ...
- SQL 视图、事务
假设看多个不同的表 select *from student ,score,course,teacher 有重复的 改为select student.Sno,sname,ssex,sbirthd ...
- RunTests.sh && RunIPhoneSecurityd.sh
https://github.com/gh-unit/gh-unit/blob/master/Scripts/RunTests.sh #!/bin/sh # If we aren't ru ...
- POJ 3140 Contestants Division (树形DP,简单)
题意: 有n个城市,构成一棵树,每个城市有v个人,要求断开树上的一条边,使得两个连通分量中的人数之差最小.问差的绝对值.(注意本题的M是没有用的,因为所给的必定是一棵树,边数M必定是n-1) 思路: ...
- mac下只遍历目录不遍历文件
install brew install tree 命令 tree -d
- cannot load such file -- bundler/setup解决
sudo gem install bundler bundle update celluloid
- vue 点击倒计时 ajax 封装
方法:function(){ var that = this; if (that.time == 0) { that.disabled = false; that.text ="点击获取&q ...
- redux form
纯粹使用react进行表单校验: class MyForm extends React.Component{ constructor(props){ super(props) this.onAddrC ...
- 暴力解说之首次部署NGINX
前言 本章基于Centos 7.x系统讲解 本章讲解下在项目上线部署的时候对NGINX的操作.有些童鞋在网上百度类似LNMP安装就跟着命令一条一条执行了,如果没报错还好,一旦报错就懵逼状态了.这是对自 ...