题目:

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+并查集的更多相关文章

  1. bzoj1604 / P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

    P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 考虑维护曼哈顿距离:$\left | x_{1}-x_{2} \right |+\left | y_{1}-y_{2} ...

  2. P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

    传送门 曼哈顿距离好像不好直接算,我们可以把牛的坐标转化一下以方便计算距离 (x,y) --> (x+y,x-y) 那么距离就可以表示成 $max(\left |x_1-x_2  \right ...

  3. 「洛谷P2906」[USACO08OPEN]牛的街区Cow Neighborhoods 解题报告

    P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 题目描述 Those Who Know About Cows are aware of the way cows gr ...

  4. 洛谷 P2909 [USACO08OPEN]牛的车Cow Cars

    传送门 题目大意: m个车道. 如果第i头牛前面有k头牛,那么这头牛的最大速度会 变为原本的速度-k*D,如果速度小于l这头牛就不能行驶. 题解:贪心 让初始速度小的牛在前面 代码: #include ...

  5. [USACO08OPEN]牛的街区Cow Neighborhoods

    题目描述: luogu 题解: 技巧题. 曼哈顿距离:$|x1-x2|+|y1-y2|$ 切比雪夫距离:$\max(|x1-x2|,|y1-y2|)$ 曼哈顿距离转切比雪夫距离:$(x,y)-> ...

  6. 洛谷P3080 [USACO13MAR]牛跑The Cow Run

    P3080 [USACO13MAR]牛跑The Cow Run 题目描述 Farmer John has forgotten to repair a hole in the fence on his ...

  7. 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  8. 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  9. Bzoj1202/洛谷P2294 [HNOI2005]狡猾的商人(带权并查集/差分约束系统)

    题面 Bzoj 洛谷 题解 考虑带权并查集,设\(f[i]\)表示\(i\)的父亲(\(\forall f[i]<i\)),\(sum[i]\)表示\(\sum\limits_{j=fa[i]} ...

随机推荐

  1. Visual Studio 智能提示功能消失解决办法

    步骤如下: 开始菜单 -->所有程序-->Visual Studio 2012文件夹 --> Visual Studio Tools --> Developer Command ...

  2. Jmeter使用之:高效组织接口自动化用例技巧

    Jmeter怎么使用的文章多如牛毛,但怎么组织好测试用例,则几乎很难看到.在本文,我将把Jmeter下怎么组织测试用例的几点心得分享给大家,希望能给你一些帮助或启示. 1.善用“逻辑控制器”中的“简单 ...

  3. 利用爬虫、SMTP和树莓派3B发送邮件&续集&(爬取墨迹天气预报信息)

    -----------------------------------------------学无止境----------------------------------------------- 前 ...

  4. Angular6项目搭建

    参照 草根专栏- ASP.NET Core + Ng6 实战:https://v.qq.com/x/page/b076702elvw.html 安装工具: Nodejs, npm     最新版, h ...

  5. HDU - 6440(费马小定理)

    链接:HDU - 6440 题意:重新定义加法和乘法,使得 (m+n)^p = m^p + n^p 成立,p是素数.,且satisfied that there exists an integer q ...

  6. (python)leetcode刷题笔记05 Longest Palindromic Substring

    5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

  7. [Clr via C#读书笔记]Cp9参数

    Cp9参数 可选参数和命名参数 参数设置了默认值(设置要从右到左,有默认值的参数必须放在没有默认值的参数的后面,默认值必须是常量),就可以使用可选参数和命名参数了.向方法传递实参的时候,编译器按照从左 ...

  8. linux NULL 的定义

    #undef NULL #if defined(__cplusplus) #define NULL 0 #else #define NULL ((void *)0) #endif

  9. maven 安装、配置

    简介: maven 大大提高项目开发速度     编译---打包---测试--安装   一条龙 maven将项目构建的过程标准化,每一个阶段使用一个命令完成,下面是构建过程一些阶段 清理 mvn cl ...

  10. python 读取 log日志的编码问题

    1.我要读取log日志的”执行成功”的个数,log日志编码格式为GBK 2.显示报错,大致意思是说utf-8的代码不能解析log日志 3.后来想想把log日志用GBK编码读出来,写到新文件中,用utf ...