[题目链接]

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. CodeForces - 592D Super M 题解

    题目大意: 一棵树 n个点 有m个点被标记 求经过所有被标记的点的最短路径的长度以及起点(如有多条输出编号最小的起点). 思路: 1.当且仅当一个点本身或其子树中有点被标记时该点在最短的路径上因此,可 ...

  2. HDU 5242 利用树链剖分思想进行贪心

    题目大意: 在给定带权值节点的树上从1开始不回头走到某个底端点后得到所有经过的点的权值后,这些点权值修改为0,到达底部后重新回到1,继续走,问走k次,最多能得到多少权值之和 这其实就是相当于每一次都走 ...

  3. D 题

    题目大意:找朋友,最好把朋友最多的一堆的人数输出 运用并查集,每次更新最大数即可: 代码: #include <iostream> #include <cstdio> #inc ...

  4. [luoguP1168]中位数(主席树+离散化)

    传送门 模板题一道,1A. ——代码 #include <cstdio> #include <algorithm> #define ls son[now][0], l, mid ...

  5. vue2.0 类淘宝不同屏幕适配及px与rem转换问题

    因为项目需要,vue开发项目,必须将已写的以px为单位的部分,转换为rem.要是全部转换,这大量的计算量,哪怕是sublime Text 的cssrem插件,也是一个庞大的工作量.所以,直接使用插件没 ...

  6. CodeForces - 356A Knight Tournament

    http://codeforces.com/problemset/problem/356/A 首先理解题意 每次给出l 和r  在l - r之间还有资格的选手中得出一个胜者 暴力思路: 首先维护还有资 ...

  7. poj2117求割点后最多的块。

    tarjan算法,枚举割点(注意此题无向图可能不连通),每个割点分割后最大块数+连通分量-1即可.开始老是TLE,后来比较了他人代码,只在vector<vector<int.>.&g ...

  8. $.post()用法例子

    1:删除用户操作 $('.delete').click(function(){ var classid=$(this).parent().siblings().eq(0).children().val ...

  9. struts2中的session使用

    1.1. 如何获取Session 1.1.1. 获取Session的方式 Struts2中获取Session的方式有3种,大家掌握其中任何一种都可以. 通过ActionContext.getConte ...

  10. easyshell 安装

    EasyShell是一个可以直接在Eclipse IDE中打开shell窗口的工具,在shell中运行选中的文件,打资源管理. 百度经验:jingyan.baidu.com 工具/原料 Easy_Sh ...