Description
Suppose you want to arrange a number of events in enough venues and want to use as few venues as possible. Design an effective greedy algorithm for scheduling. ( 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 . )
Requirement: For a given K to be scheduled activities, the programming calculates the minimum venue schedule.
Input
input data 1 a positive integer k (k<=10000) k k 2 k 0
Output
The output data is 1 rows, indicating the minimum number of venues calculated.
Sample Input5 1 (+)Sample Output3
Idea: greedy, first sort all activities according to the start time, then mark the adjacent non-coincident activities in turn. The number of times to go back and forth is the amount of venues needed. Note: The start time of an activity can be exactly the end time of the previous activity.
Code:
#include <iostream>#include<vector>#include<stdlib.h>#include<algorithm>using namespacestd;classactivity{ Public: intstart; intend; BOOLprocessed; Activity (intStartintEndBOOLProcessed=false){ This->start=start; This->end=end; This->processed=processed; }};BOOLcmpConstActivity & A,ConstActivity &b) { returnB.start>A.start;}intGreedy (vector<activity>activities) { intlen=activities.size (); intret=0; while(1){ BOOL Get=false; for(intI=0; i<len;i++){ intTime ; if(!activities[i].processed) {ret++; time=Activities[i].end; Activities[i].processed=true; } for(intj=i+1; j<len;j++){ if(!activities[j].processed&&activities[j].start>=Time ) {activities[j].processed=true; time=Activities[j].end; } } } if(!Get) Break; } returnret;}intMainintargcChar**argv) { intN; CIN>>N; Vector<activity>activities; for(intI=0; i<n;i++){ intstart; intend; CIN>>start>>end; Activity Temp=activity (start,end); Activities.push_back (temp); } sort (Activities.begin (), Activities.end (), CMP); cout<<greedy (Activities) <<Endl;}
Venue arrangement Questions