"Problem description"
Suppose you want to arrange a number of events in enough venues and want to use as few venues as possible. (This problem is actually a well-known graph coloring issue.) If each activity is a vertex of the graph, the incompatible activities are connected by edges. Causes the adjacent vertices to have a minimum number of colors, corresponding to the minimum number of venues to find. )
"Data Entry"
The first line has 1 positive integer k, which indicates that there are k pending activities. In the next K-line, there are two positive integers per line, representing the start and end times of the K-pending activity. The time starts at 0 minutes.
"Analysis and Solution"
Set the given n activity 1,2,....,n, their start time and end time are s[i] and f[i],i=1~n respectively, and F[1]<f[2]<.....<f[n].
(1) It is easy to think of the algorithm greedyselector (event scheduling problem) to arrange the venue. In the worst case algorithm requires O (n2) computation time.
(2) In fact, a more efficient algorithm can be devised. n activity 1,2,....,n as n semi-closed active range [S[i],f[i]],i=1~n] on a real line. The problem discussed is actually the maximum number of overlapping numbers for the n semi-closed interval. Because overlapping activity zones correspond to activities that are incompatible. If the maximum overlap number of these n active intervals is m, then the activity of the M overlapping intervals is incompatible, so at least the M-hall will be arranged to accommodate the M-event.
In order to effectively arrange these n activities, first sort the 2n endpoints of n active intervals. The entire line is then scanned from left to right using the scanning algorithm. At each event point (that is, the start or end of the activity) for the venue arrangement. When encountering a start time s[i], the activity I is arranged in a free venue. Encountered an end time F[i], the event I occupied the venue released to the free conference stack, is ready to use. After such a scan, the maximum number of M venues (M is the maximum overlap interval) is arranged. The arrangement of the venues is therefore optimal. The calculation time required for the above algorithm is mainly used to sort the 2n interval endpoints, which requires O (NLOGN) computation time.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct NODE
{
int data;//time value
int FLAG;//1 represents start time, 0 indicates end time
}node;
BOOL Compare (node n1, node N2)
{
return n1.data<n2.data;//ascending order
}
int Maxrepeat (vector<node > V)//MAX overlap number
{
int curr=0, sum=0;
Vector<node>::iterator It=v.begin ();
for (; It<=v.end (); it++)
{
if ((*it). Flag)
{
curr++;
if (curr>sum)
sum=curr;
}
else
curr--;
}
return sum;
}
void Main ()
{
int i, n;
Vector<node> v;
cin>>n;
Node T;
for (i=1;i<=2*n; i++)
{
cin>>t.data;
t.flag=i%2;
V.push_back (t);
}
Sort (V.begin (), v.end (), compare);
Cout<<maxrepeat (v) <<endl;
}