Question Link
Question:Can Pastor John arrange for all weddings to send a blessing to all weddings (The Blessing time is half the wedding time ).
Ideas: Greedy, sort the wedding period from small to large, and try to end the wedding as early as possible. The last time the wedding blessing starts (each wedding has a Blessing time that begins at the latest) is earlier.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 100005; struct wedding {long S, T, mid, L;} W [maxn]; int N, flag; int CMP (wedding A, wedding B) {return. mid <B. mid;} int solve () {int start = 0; For (INT I = 0; I <n; I ++) {If (Start> W [I]. t-W [I]. l) return false; else if (start <= W [I]. s) Start = W [I]. mid; else start + = W [I]. l;} return true;} int main () {While (scanf ("% d", & N) {for (INT I = 0; I <N; I ++) {scanf ("% LLD", & W [I]. s, & W [I]. t); W [I]. L = (W [I]. t-W [I]. s)/2 + 1; W [I]. mid = W [I]. S + W [I]. l;} Sort (W, W + N, CMP); solve (); If (solve () printf ("Yes \ n "); else printf ("NO \ n");} return 0 ;}