NC24084. [USACO 2017 Dec G]A Pie for a Pie
描述
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 (). 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 00, in which case the gift exchange will end and both cows will be happy.
Note that a pie may not be gifted twice, nor can either cow return a pie gifted to her.
For each of the N pies Bessie could select as her initial gift to Elsie, determine the minimum number of pies that could possibly be gifted in the resulting exchange before the cows are happy.
输入描述
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 .
输出描述
There 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.
示例1
输入:
2 1 1 1 5 0 4 2 1 4
输出:
3 1
C++ 解法, 执行用时: 541ms, 内存消耗: 12668K, 提交时间: 2021-12-20 20:03:46
#include <bits/stdc++.h> #define _for(i, a, b) for (int i = (a); i < (int)(b); ++i) using namespace std; const int NN = 1e5 + 4; int N, T, A[2 * NN], B[2 * NN], D[2 * NN]; struct cmpA { inline bool operator()(int a, int b) const { return B[a] < B[b]; } }; struct cmpB { inline bool operator()(int a, int b) const { return A[a] < A[b]; } }; int main() { ios::sync_with_stdio(false), cin.tie(0); cin >> N >> T; _for(i, 0, 2 * N) cin >> A[i] >> B[i], A[i] = -A[i], B[i] = -B[i], D[i] = -1; queue<int> Q; multiset<int, cmpA> sA; multiset<int, cmpB> sB; _for(i, 0, N) { if (B[i] == 0) Q.push(i), D[i] = 1; else sA.insert(i); if (A[N + i] == 0) Q.push(N + i), D[N + i] = 1; else sB.insert(N + i); } while (!Q.empty()) { int i = Q.front(); Q.pop(); if (i < N) while (true) { auto p = sB.lower_bound(i); if (p == sB.end() || A[*p] > A[i] + T) break; D[*p] = D[i] + 1, Q.push(*p), sB.erase(p); } else while (true) { auto p = sA.lower_bound(i); if (p == sA.end() || B[*p] > B[i] + T) break; D[*p] = D[i] + 1, Q.push(*p), sA.erase(p); } } _for(i, 0, N) cout << D[i] << endl; }