洛谷 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]} ...
随机推荐
- vs2015 mvc项目数据迁移报错
第一次做个mvc项目玩玩,然后需要数据迁移,也没做过,就百度找怎么数据迁移, 找到的方法是: 如果数据是在类库项目里就在‘程序包管理控制台’输入:enable-migrations -ContextT ...
- 「日常训练」Two Substrings(Codeforces Round 306 Div.2 A)
题意与分析 一道非常坑的水题.分析醒了补. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back ...
- RabbitMQ基础教程之使用进阶篇
RabbitMQ基础教程之使用进阶篇 相关博文,推荐查看: RabbitMq基础教程之安装与测试 RabbitMq基础教程之基本概念 RabbitMQ基础教程之基本使用篇 I. 背景 前一篇基本使用篇 ...
- Git与远程仓库关联以及关联错误解决方法
假设你github的用户名是 helloworld ,你在上面创建了一个 名为 hello 的 repository. 一. 与本地仓库进行关联 1.1用原生ssh进行关联,速度快: git re ...
- 小球下落 (Dropping Balls,UVA 679)
题目描述: 题目思路: 1.直接用数组模拟二叉树下落过程 //超时 #include <iostream> #include <cstring> using namespace ...
- CSP201312-2:ISBN号码
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- java对json文件的操作
第一步:通过FileReader读取json文件第二步:使用BufferReader,先通过I/O读取一定大小的数据缓存到数组中,然后再从数组取出数据.第三步:用一个字符串把每次传来的数据处理后写到新 ...
- CDH问题集
1.在CM中添加主机报JDK错误 手动在机器上安装oracle-jdk1.7+update64.然后在CM中选择不安装oracle-jdk即可. 2.HostMoinitor无法与server联系 查 ...
- POJ 2208 Pyramids(求四面体体积)
Description Recently in Farland, a country in Asia, a famous scientist Mr. Log Archeo has discovered ...
- vue.js学习之 跨域请求代理与axios传参
vue.js学习之 跨域请求代理与axios传参 一:跨域请求代理 1:打开config/index.js module.exports{ dev: { } } 在这里面找到proxyTable{}, ...