洛谷 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]} ...
随机推荐
- Appium安装教程
一.适用操作系统Win7 旗舰版Sp1 64位操作系统 或 32位操作系统二.所需软件jdk-7u45-windows-i586.exenode-v0.10.28-x86.msi (32位)下载地址: ...
- ant-design学习准备_1
在学习ant-desin过程中,发现很多知识都不清楚,从现在开始,每天将自己学习到的知识进行一个总结记录,前端大佬勿扰勿喷.先介绍几个基础概念和一些常用命令: 1.什么是脚手架 我们经常在各个博客论坛 ...
- JavaScript 常用正则示例
1. trim功能(清除字符串两端空格) String.prototype.trim = function() { return this.replace(/(^\s+)|(\s+$)/g, '') ...
- matlab画图:设置y轴位置,使y轴在x轴的中间
sigmoid函数图像 x=-10:0.1:10; y=sigmf(x,[1 0]); plot(x,y) 画出的图像如下所示: 怎么将Y轴放在中间呢,而不是在左边? 即如何得到这种效果呢? 方法 ...
- 1208: [HNOI2004]宠物收养所
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 12030 Solved: 4916 Description ...
- Java算法2
实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 分析:若从前向后遍历的话,那Happy后面 ...
- Dictionary tabPage使用
public override bool AccptChange() { //if (oldvalue == null || oldvalue.Count <= 0) //{ // return ...
- jdk1.8新特性-Lambda表达式使用要点
前言 在jdk1.8出来的时候看到过,没怎么了解.但是最近再看kafka和spark框架,框架示例中ava版的很多地方用到Lambda表达式,发现使用Lambda表达式代码确实简单了好多,有些例子大致 ...
- 从oracle导入hive
sqoop import --connect jdbc:oracle:thin:@10.39.1.43:1521/rcrm --username bi_query --password ####### ...
- BZOJ 4557 JLOI2016 侦查守卫 树形dp
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4557 题意概述: 给出一棵树,每个点付出代价w[i]可以控制距离和它不超过d的点,现在给 ...