洛谷 P2906 [USACO08OPEN]牛的街区Cow Neighborhoods | Set+并查集
题目:
https://www.luogu.org/problemnew/show/P2906
题解:
垃圾水题
#include<cstdio>
#include<algorithm>
#include<set>
#define N 100005
typedef long long ll;
using namespace std;
struct node
{
ll x,y,id;
bool operator < (const node &a)const {return y<a.y || (y==a.y && id<a.id);}
}p[N];
multiset <node> s;
multiset <node> :: iterator it;
ll n,c,q[N],l,r=-,fa[N],sz[N],ans,mx;
bool cmp(node a,node b) {return a.x<b.x || (a.x==b.x && a.y<b.y);}
ll find(ll x) {return fa[x]=fa[x]==x?x:find(fa[x]);}
void merge(ll i,ll j)
{
ll x=find(i),y=find(j);
if (x==y) return;
if (sz[x]>sz[y]) swap(x,y);
fa[x]=y,sz[y]+=sz[x];
}
int main()
{
scanf("%lld%lld",&n,&c);
for (ll i=,x,y;i<=n;i++)
scanf("%lld%lld",&x,&y),p[i].x=x+y,p[i].y=x-y,p[i].id=fa[i]=i,sz[i]=;
sort(p+,p++n,cmp);
for (ll i=;i<=n;i++)
{
q[++r]=i;
while (l<r && p[q[r]].x-p[q[l]].x>c) s.erase(p[q[l]]),l++;
it=s.insert(p[i]);
if (it!=s.begin())
if ((p[i].y-(*(--it)).y<=c)) merge(p[i].id,((*it).id)),it++;
else it++;
if (++it!=s.end() && (*(it)).y-p[i].y<=c )
merge(p[i].id,(*it).id);
}
for (ll i=;i<=n;i++)
if (find(i)==i) ans++,mx=max(mx,sz[i]);
printf("%lld %lld\n",ans,mx);
return ;
}
洛谷 P2906 [USACO08OPEN]牛的街区Cow Neighborhoods | Set+并查集的更多相关文章
- bzoj1604 / P2906 [USACO08OPEN]牛的街区Cow Neighborhoods
P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 考虑维护曼哈顿距离:$\left | x_{1}-x_{2} \right |+\left | y_{1}-y_{2} ...
- P2906 [USACO08OPEN]牛的街区Cow Neighborhoods
传送门 曼哈顿距离好像不好直接算,我们可以把牛的坐标转化一下以方便计算距离 (x,y) --> (x+y,x-y) 那么距离就可以表示成 $max(\left |x_1-x_2 \right ...
- 「洛谷P2906」[USACO08OPEN]牛的街区Cow Neighborhoods 解题报告
P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 题目描述 Those Who Know About Cows are aware of the way cows gr ...
- 洛谷 P2909 [USACO08OPEN]牛的车Cow Cars
传送门 题目大意: m个车道. 如果第i头牛前面有k头牛,那么这头牛的最大速度会 变为原本的速度-k*D,如果速度小于l这头牛就不能行驶. 题解:贪心 让初始速度小的牛在前面 代码: #include ...
- [USACO08OPEN]牛的街区Cow Neighborhoods
题目描述: luogu 题解: 技巧题. 曼哈顿距离:$|x1-x2|+|y1-y2|$ 切比雪夫距离:$\max(|x1-x2|,|y1-y2|)$ 曼哈顿距离转切比雪夫距离:$(x,y)-> ...
- 洛谷P3080 [USACO13MAR]牛跑The Cow Run
P3080 [USACO13MAR]牛跑The Cow Run 题目描述 Farmer John has forgotten to repair a hole in the fence on his ...
- 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- Bzoj1202/洛谷P2294 [HNOI2005]狡猾的商人(带权并查集/差分约束系统)
题面 Bzoj 洛谷 题解 考虑带权并查集,设\(f[i]\)表示\(i\)的父亲(\(\forall f[i]<i\)),\(sum[i]\)表示\(\sum\limits_{j=fa[i]} ...
随机推荐
- mybatis interceptor 处理查询参数及查询结果
拦截器:拦截update,query方法,处理查询参数及返回结果. /** * Created by windwant on 2017/1/12. */ @Intercepts({ @Signatur ...
- JVM之G1收集器
Garbage-First,面向服务端的垃圾收集器. 并行与并发:充分利用多核环境减少停顿时间, 分代收集:不需要配合其它收集器 空间整合:整体上看属于标记整理算法,局部(region之间)数据复制算 ...
- JavaScript 常用正则示例
1. trim功能(清除字符串两端空格) String.prototype.trim = function() { return this.replace(/(^\s+)|(\s+$)/g, '') ...
- (python)leetcode刷题笔记 01 TWO SUM
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- error:no module named StringIO or cStringIO
一般遇到没有某个模块问题的时候,通常的解决方法是pip相应的模块: 不过,鉴于Python2和python3的不同(让人头疼) 解决方法:在python3中,该模块被新的模块取代,即io. 重新imp ...
- 【转载】图解Java常用数据结构(一)
图解Java常用数据结构(一) 作者:大道方圆 原文:https://www.cnblogs.com/xdecode/p/9321848.html 最近在整理数据结构方面的知识, 系统化看了下Jav ...
- JS中Document节点总结
document对象是documentHTML的一个实例,也是window对象的一个属性,因此可以将document对象作为一个全局对象来访问. Document节点的子节点可以是DocumentTy ...
- Java学习个人备忘录之关键字static
被static标记的东西会放在内存中被共享的,对象用到时,就会来取的. class Person { String name; //成员变量,实例变量 static String country = ...
- Huffuman树
问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, pn-1},用这列数构造Huffman树的过程如下: 1. ...
- 文件异步上传-ajaxFileUpload
$.ajaxFileUpload是一个jquery插件 文章:jQuery插件之ajaxFileUpload