http://hihocoder.com/problemset/problem/1309
The main idea is to give the start time of n tasks, and ask for the minimum number of machines.
There is a greedy strategy is that if the end of a task, it is necessary to take a start time closest to this is more cost-effective. We assume a task pool, then the first end of the task pool is bound to take the first game in the remaining tasks (the earliest end of the same start time), and assume that the task is p, then for an end time later than p Task, obviously the latter period of time is wasted, also for a start time later than the task of P , the previous paragraph is wasteful;
The key to
p p task q assuming q x Span style= "font-family: the song Body;" > But then the p After, unable to pick up x
first, for the connection p q and x , unable to connect, may need to re-open a machine, or there is another task in the task pool can be connected. For the p Can't connect, maybe need to re-open a machine, maybe there is a task in the task pool can be connected.
For cases where a machine needs to be re-opened, the effect is equivalent. Then assume that the case of Q , the task pool exists another task can be connected to P, bound to the case of P , there is the same task can be answered Q , because The start time of P is earlier than Q, the two are also equivalent in this case.
So, in summary, the first game in the remaining tasks is advantageous (the earliest end of the same start time).
So you just start to sequence all tasks by the start time, and the times are ordered by the end time. You then maintain the task pool with a priority queue (heap).
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<vector>#include<string>#defineLL Long Longusing namespacestd;Const intMAXN =100005;structnode{ints, E;} A[MAXN];BOOLCMP (node x, node Y) {if(X.s! = Y.S)returnX.s <Y.s; Else returnX.E <y.e;}intN;voidWork () {priority_queue<int, vector<int, greater<int> >Q; Q.push (0); intK; for(inti =0; I < n; ++i) {k=Q.top (); Q.pop (); if(k <=a[i].s) Q.push (A[I].E); ElseQ.push (A[I].E), Q.push (k); } printf ("%d\n", Q.size ());}intMain () {//freopen ("test.in", "R", stdin); //freopen ("Test.out", "w", stdout); while(SCANF ("%d", &n)! =EOF) { for(inti =0; I < n; ++i) scanf ("%d%d", &a[i].s, &A[I].E); Sort (A, a+N, CMP); Work (); } return 0;}View Code
ACM Learning Process-hihocoder1309 Task assignment (sort && greedy)