Venue arrangement Questions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.