UV-1420 Priest John & #39; s Busiest Day
There is a ceremonies that host n weddings and give the start time and end time of the wedding. Each wedding takes more than half of the time as the ceremony, and the ceremony cannot be terminated. Ask if the ceremonies can host n weddings.
Solution: greedy. In order to host as many weddings as possible, every ceremony time must be as short as possible d = (t-s)/2 + 1 (because it must be greater than half, so add 1 ). Then, sort t-d according to the last ending time of each wedding. (to satisfy all the weddings, try to solve the early ceremony and free up time for the subsequent wedding ), maintain a time value.
#include
#include using namespace std;struct Wedding { int S, T, D; bool operator < (const Wedding& a) const { return T - D < a.T - a.D; }} W[100010];bool judge(int N) { int cur = 0; for (int i = 0; i < N; i++) { cur = max(cur, W[i].S) + W[i].D; if (cur > W[i].T) return false; } return true;}int main() { int N; while (scanf(%d, &N) && N) { for (int i = 0; i < N; i++) { scanf(%d%d, &W[i].S, &W[i].T); W[i].D = (W[i].T - W[i].S) / 2 + 1; } sort(W, W + N); printf(%s, judge(N) ? YES : NO); } return 0;}