[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=5140

[算法]

最短路

时间复杂度 : O(N^2)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + ; struct info
{
int x , y , id;
} a[MAXN << ] , b[MAXN << ]; int n , d;
int dist[MAXN << ];
vector< int > G[MAXN << ];
queue< int > q; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline bool cmpA(info a,info b)
{
return a.x < b.x;
}
inline bool cmpB(info a,info b)
{
return a.y < b.y;
} int main()
{ read(n); read(d);
for (int i = ; i <= n; i++)
{
read(a[i].x);
read(a[i].y);
}
for (int i = ; i <= n; i++)
{
read(b[i].x);
read(b[i].y);
}
memset(dist,,sizeof(dist));
for (int i = ; i <= n; i++)
{
if (a[i].y == )
{
q.push(i);
dist[i] = ;
}
a[i].id = i;
}
for (int i = ; i <= n; i++)
{
if (b[i].x == )
{
q.push(i + n);
dist[i + n] = ;
}
b[i].id = i + n;
}
sort(a + ,a + n + ,cmpA);
sort(b + ,b + n + ,cmpB);
for (int i = ; i <= n; i++)
{
int l = , r = n , pos = -;
while (l <= r)
{
int mid = (l + r) >> ;
if (b[mid].y >= a[i].y)
{
pos = mid;
r = mid - ;
} else l = mid + ;
}
if (pos == -) continue;
for (int j = pos; j <= n; j++)
{
if (b[j].y > a[i].y + d) break;
G[b[j].id].push_back(a[i].id);
}
}
for (int i = ; i <= n; i++)
{
int l = , r = n , pos = -;
while (l <= r)
{
int mid = (l + r) >> ;
if (a[mid].x >= b[i].x)
{
pos = mid;
r = mid - ;
} else l = mid + ;
}
if (pos == -) continue;
for (int j = pos; j <= n; j++)
{
if (a[j].x > b[i].x + d) break;
G[a[j].id].push_back(b[i].id);
}
}
while (!q.empty())
{
int cur = q.front();
q.pop();
for (unsigned i = ; i < G[cur].size(); i++)
{
int v = G[cur][i];
if (dist[v] != -) continue;
dist[v] = dist[cur] + ;
q.push(v);
}
}
for (int i = ; i <= n; i++) printf("%d\n",dist[i]); return ; }

[Usaco2017 Dec] A Pie for a Pie的更多相关文章

  1. 【BZOJ5138】[Usaco2017 Dec]Push a Box(强连通分量)

    [BZOJ5138][Usaco2017 Dec]Push a Box(强连通分量) 题面 BZOJ 洛谷 题解 这题是今天看到萝卜在做然后他一眼秒了,我太菜了不会做,所以就来做做. 首先看完题目,是 ...

  2. BZOJ5142: [Usaco2017 Dec]Haybale Feast(双指针&set)(可线段树优化)

    5142: [Usaco2017 Dec]Haybale Feast Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 182  Solved: 131[ ...

  3. BZOJ5142: [Usaco2017 Dec]Haybale Feast 线段树或二分答案

    Description Farmer John is preparing a delicious meal for his cows! In his barn, he has NN haybales ...

  4. [BZOJ5139][Usaco2017 Dec]Greedy Gift Takers 权值线段树

    Description Farmer John's nemesis, Farmer Nhoj, has NN cows (1≤N≤10^5), conveniently numbered 1…N. T ...

  5. BZOJ5137[Usaco2017 Dec]Standing Out from the Herd

    看了半天题 不知道怎么用SAM维护 于是借(chao)鉴(xi)的一发神犇的 只要判断这个子串之前被标记的记号(也就是他属于第几个串)和这次转移到的是否相同 如果不同就说明该子串属于多个串 直接标记- ...

  6. bzoj5138 [Usaco2017 Dec]Push a Box

    题目描述: bz luogu 题解: 暴力可以记录$AB$位置转移,这个时候状态是$n^4$的,无法接受. 考虑只记录$A$在$B$旁边时的状态,这个时候状态时$n^2$的. 所以说转移有两种,一种是 ...

  7. BZOJ5137: [Usaco2017 Dec]Standing Out from the Herd(广义后缀自动机,Parent树)

    Description Just like humans, cows often appreciate feeling they are unique in some way. Since Farme ...

  8. BZOJ 5137: [Usaco2017 Dec]Standing Out from the Herd(后缀自动机)

    传送门 解题思路 这个似乎和以前做过的一道题很像,只不过这个是求本质不同子串个数.肯定是先把广义\(SAM\)造出来,然后\(dfs\)时把子节点的信息合并到父节点上,看哪个只被一个串覆盖,\(ans ...

  9. tcpdump for android L 5.x with pie support

    由于使用了NDK编译的可执行文件在应用中调用,在4.4及之前的版本上一直没出问题. 最近由于要测试在Android L上的运行情况发现,当运行该可执行文件时,报如下错误: error: only po ...

随机推荐

  1. HDU 1102 Kruscal算法

    题目大意:给定村庄的数量,和一个矩阵表示每个村庄到对应村庄的距离,矩阵主对角线上均为1 在给定一个数目Q,输入Q行之间已经有通道的a,b 计算还要至少修建多少长度的轨道 这道题目用Kruscal方法进 ...

  2. 没有上司的舞会(hdu 1520)

    题目描述 Description Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现在有个周年庆宴会 ...

  3. Codeforces Round #297 (Div. 2) [ 折半 + 三进制状压 + map ]

    传送门 E. Anya and Cubes time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  4. iOS-bug·Failed to connect to github-production-release-asset-2e65be.s3.amazonaws.com port 443: Operation time out

    在首次运行 react naticve 项目时, 遇到了如下的问题: Failed to connect to github-production-release-asset-2e65be.s3.am ...

  5. CodeForces 596A Wilbur and Swimming Pool

    水题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> u ...

  6. Sublime3 Preference, Settings-User

    {"font_face": "Consolas","font_size": 15,"ignored_packages": ...

  7. linux安装mail服务使用外部MTA发送邮件

    阉割版的linux没有mail命令,也没有/etc/mail.rc文件 需要安装mail服务 yum install mailx.x86_64 几个概念:MUA.MTA.MDA 邮件用户代理(MUA, ...

  8. 【转】Web Worker javascript多线程编程(一)

    原文:https://www.cnblogs.com/peakleo/p/6218823.html -------------------------------------------------- ...

  9. App中显示html网页

    在现在的移动开发中,越来越多的web元素增加到了app里面,hybrid app可以综合native app 和 web app的长处,可以通过webView实现 htmllayout.xml: &l ...

  10. hdu1181 dfs搜索之变形课

    原题地址 这道题数据据说比較水,除了第一组数据是Yes以外.其余都是No.非常多人抓住这点就水过了.当然了,我认为那样过了也没什么意思.刷oj刷的是质量不是数量. 这道题从题目上来看是个不错的 搜索题 ...