Description
Bessie and Elsie have each baked N pies (1≤N≤10^5). Each of the 2N pies has a tastiness value according to Bessie and a (possibly different) tastiness value according to Elsie. Bessie is thinking about giving one of her pies to Elsie. If Elsie receives a pie from Bessie, she will feel obligated to give one of her pies to Bessie. So as to not appear stingy nor flamboyant, Elsie will try to pick a pie that is at least as tasty (in Elsie’s eyes) as the pie she received, but no more than D units tastier (0≤D≤10^9). Such a pie may not exist, in which case Elsie will adopt a pseudonym and exile herself to Japan.

But if Elsie does give Bessie a pie in return, Bessie will similarly try to give Elsie a pie which is at least as tasty but no more than D units tastier (in Bessie’s eyes) as the pie Elsie just gave her. Should this be impossible, Bessie too will exile herself. Otherwise she will give her chosen pie to Elsie. This cycle will continue until one of the cows is exiled, an unhappy outcome, or one of the cows receives a pie which she accords a tastiness value of 0, in which case the gift exchange will end and both cows will be happy.
Bessie和Elsie各自烤了 N(1≤N≤10^5)个馅饼)。Bessie 会这 2N 个馅饼打分,Elsie 也会。二者的打分均为一个<=10^9的非负整数。由于她们口味不同,每个派的两个分数可能不同。 她们想互赠礼物。开始时,Bessie 送给 Elsie 一个馅饼。她们收到礼物(对方做的馅饼)后都会回赠对方一个自己做的馅饼。 她们选择回礼的方法相同。以 Elsie 为例,Elsie 根据自己的打分来选择回礼。回礼的分数至少要大于等于她收到的馅饼的分数,但两个馅饼的分数差不能大于 D(0≤D≤10^9) 。如果有多个馅饼满足要求,Elsie 可以选择其中的任意一个作为回礼。若没有馅饼满足要求,Elsie 会放弃。Bessie 选择回礼的方法同理。 她们之间的礼物交换将持续到一头牛放弃(Bad End),或一头牛收到一个她自己打分为 00 的馅饼,此时礼物交换愉快结束(Happy End)。 请注意,不能把一个馅饼赠送两次,不能把馅饼送给自己。 Bessie 想知道:对于每个她做的馅饼,如果她将这个馅饼作为最开始送给 Elsie 的礼物,她俩至少要互赠多少次馅饼(Bessie 给 Elsie 算一次,Elsie 回赠 Bessie 又算一次),才能 Happy End 。如果不可能 Happy End,请输出 -1。
Input
The first line contains the two integers N and D. The next 2N lines contain two space-separated integers each, respectively denoting the value of a particular pie according to Bessie, and the value of that pie according to Elsie.

The first N lines refer to Bessie’s pies, and the remaining N lines refer to Elsie’s pies.

It is guaranteed that all tastiness values are in the range [0,10^9].
第一行有两个整数 N, D。
在接下来的 2N行中,每行有两个整数,表示对于某个馅饼,Bessie 的打分和 Elsie 的打分。其中,前 N 行表示 Bessie 做的馅饼,后 N行则是 Elsie 做的馅饼。
Output
here should be N lines in the output. Line i should contain a single integer: the minimum number of pies that could be gifted in a happy gift exchange started with Bessie’s pie i. If no gift exchange starting with pie i is happy, then line i should contain the single integer −1 instead.
输出共 N 行,每行一个整数。 第 i 行的整数表示:如果 Bessie 将馅饼 i作为最开始送给 Elsie 的礼物,她俩至少要互赠多少次馅饼才能 Happy End 。如果不可能 Happy End,请在该行输出 -1。
Sample Input
2 1
1 1
5 0
4 2
1 4
Sample Output
3
1
Solution
最短路。两个不是同一个人做的馅饼,如果某个家伙把馅饼i给了对方,对方可以用馅饼j送回去,就把i和j连边。然后跑一遍最短路。答案就是最短距离。新建一个0点,跟所有被评为0的馅饼连边,以0为源点跑最短路。
建图时用二分,或者排序优化。

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int N=100005;
int n,m,cnt=0,head[N*2],dis[N*2];
struct node {int to,next;
}edge[1500005];
struct rec {int x,y,id;
}a[N],b[N];
queue <int> q;
void add(int x,int y) {edge[cnt].to=y;edge[cnt].next=head[x];head[x]=cnt++;
}
bool cmpa(rec x,rec y) {return x.x<y.x;
}
bool cmpb(rec x,rec y) {return x.y<y.y;
}
void spfa() {while(!q.empty()) {int now=q.front();q.pop();for(int i=head[now];i!=-1;i=edge[i].next) {int to=edge[i].to;if(dis[to]!=-1)continue;dis[to]=dis[now]+1;q.push(to);}}
}
int main() {freopen("piepie.in","r",stdin);freopen("piepie.out","w",stdout);scanf("%d%d",&n,&m);memset(head,-1,sizeof(head));for(int i=1;i<=n*2;i++)dis[i]=-1;for(int i=1;i<=n;i++) {scanf("%d%d",&a[i].x,&a[i].y);if(a[i].y==0)dis[i]=1,q.push(i);a[i].id=i;}for(int i=1;i<=n;i++) {scanf("%d%d",&b[i].x,&b[i].y);if(b[i].x==0)dis[i+n]=1,q.push(i+n);b[i].id=i+n;}sort(a+1,a+1+n,cmpa);sort(b+1,b+1+n,cmpb);for(int i=1;i<=n;i++) {int l=1,r=n,ret=-1;while(l<=r) {int mid=(l+r)>>1;if(b[mid].y>=a[i].y)ret=mid,r=mid-1;else l=mid+1;}if(ret==-1)continue;for(int j=ret;j<=n;j++) {if(b[j].y>a[i].y+m)break;add(b[j].id,a[i].id);}}for(int i=1;i<=n;i++) {int l=1,r=n,ret=-1;while(l<=r) {int mid=(l+r)>>1;if(a[mid].x>=b[i].x)ret=mid,r=mid-1;else l=mid+1;}if(ret==-1)continue;for(int j=ret;j<=n;j++) {if(a[j].x>b[i].x+m)break;add(a[j].id,b[i].id);}}spfa();for(int i=1;i<=n;i++)printf("%d\n",dis[i]);fclose(stdin);fclose(stdout);return 0;
}

【USACO 2017 December Gold】A Pie for a Pie题解相关推荐

  1. [USACO 2017 Feb Gold] Tutorial

    Link: 传送门 A: 分层图最短路(其实就是最短路转移时多记录一维的数据 #include <bits/stdc++.h>using namespace std; #define X ...

  2. USACO 2017 December Contest Platinum T3: Greedy Gift Takers

    题目大意 有 N(1≤N≤1e5)头牛按顺序排成一列,编号从 1 到 N,1 号牛在队头,N 号牛在队尾. 每次位于队头的牛 i 拿到一个礼物,然后插入到从队尾数ci​头牛之前的位置..举个栗子: 初 ...

  3. BSOJ4217 【USACO 2013 Feburary Gold】旅行线路 DP(双路递推)

    4217 -- [USACO 2013 Feburary Gold]旅行线路 Description 贝西经营着一家旅行社,一天贝西带着几队游客沿着亚马逊河旅行,河的两边分布着一些景点,每个景点都对应 ...

  4. [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)

    \(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...

  5. 【USACO】2017 December Contest, Platinum题解

    [比赛经历] 大概顺利满分了,就是T2的代码比较难调. T2能够直观地反映出GDB和输出调试结合的优越性. [T1]Standing Out from the Herd [题目链接] 点击打开链接 [ ...

  6. USACO 2022 December Contest, BronzeProblem 1. Cow College 题解

    以下是2022年-2023年USACO赛季第一个月的青铜组第一题,可以使用"计数数组+打擂台找最值"的思想,需要考生掌握一点算法才能通关.文章引用了官网题面,提供思考思路和代码,代 ...

  7. USACO 2019 December Silver】MooBuzz

    各位苦闷的父老乡亲们, 如果有问题,可以私信 如果需要测试自己的题,点这儿 阅读之前,不妨先点个赞呗. 正文 题目描述 Farmer John 的奶牛们最近成为了一个简单的数字游戏"Fizz ...

  8. BZOJ 1589 Trick or Treat on the Farm (tarjan缩点,记忆化搜索)[Usaco 2008 Dec Gold]【BZOJ计划】

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Weblink https://hydro.ac/d/bzoj/p/1589 Problem 每年万圣 ...

  9. BZOJ3075[USACO 2013 Mar Gold 3.Necklace]——AC自动机+DP

    题目描述 给你一个长度为n的字符串A,再给你一个长度为m的字符串B,求至少在A中删去多少个字符才能使得B不是A的子串.注:该题只读入A和B,不读入长度,先读入A,再读入B.数据保证A和B中只含小写字母 ...

最新文章

  1. 尚学堂java 答案解析 第五章
  2. list对象排序java_【Java】list对象(类)按某个属性排序
  3. DRF (Django REST framework) 框架介绍(3)
  4. 0002-Add Two Numbers(两数相加)
  5. 软件设计师 - 计算机组成体系结构 -
  6. java获取网络图片_有了这50套Java毕设项目(源码 案例),offer拿到手软,无偿分享...
  7. php充值注入,PHP注入一路小跑
  8. Python实现matplotlib显示中文的方法详解
  9. 重写equals方法(未完)
  10. RC延时电路延时计算
  11. 10年老程序员:到底是学前端好还是后端好?
  12. win10局域网访问其他计算机名,教你win10两台电脑怎么连接局域网
  13. 手把手带你入门 API 开发
  14. 一年级的计算机你,写电脑的一年级作文五篇
  15. AT2402E射频前端单芯片替代RFX2402E
  16. 用来用去,Python脚本打包 exe还是这款工具最棒
  17. APP开发从需求到产品—APP产品经理成长日记
  18. Windows7自带截图工具没法保存
  19. hcip难不难?华为认证考试难不难?
  20. windows 建立窗口的程序代码

热门文章

  1. Python 每天定时运行某程序代码
  2. 网络资源下载方式:http/https、ftp/sftp、BT种子、磁力下载、ed2k下载等的区别
  3. C# 学习随笔 获取计算机硬件参数,包括(CPU ID,MAC,磁盘ID和IP地址)
  4. 前端终端组件xterm.js的使用(转)
  5. Linux从入门到秃头
  6. 浙江八年级 python_今年9月起,浙江八年级新增Python编程课程
  7. office2007 向程序发送命令时出现问题 的解决办法
  8. 全志F1C600芯片处理器介绍
  9. c语言中f1是什么意思啊,F1知识科普,这些字母代表什么你知道吗?
  10. 《自然语言处理实践——聊天机器人技术原理与应用》(王昊奋)简要笔记(全)