Venue arrangement Question 4-1

Source: Internet
Author: User

* Problem 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 in a straight line. Causes the neighboring points to have a minimum number of shades of different colors, corresponding to the minimum number of venues to find.

Input. TXT output. Txt

5 3

1 23

12 28

25 35

27 80

36 50

The idea to solve this problem is: I need to get the minimum number of venues, so my most concern is the start time of each activity, and then the end time. Why do you say that? Because greed is to let each venue to arrange as many activities as possible, when I put the start time in accordance with the non-subtraction sequence, I can intuitively determine the current this activity belongs to which venue, and because the non-reduction sequence to achieve the purpose of "use", that is, each venue to arrange as many activities as possible.

This assumes that the start time is already sequenced, so what should be done next? (beating myself, refreshing) I use a variable num to record the number of venues that have been arranged, and then use a "num" to record the sequence number of the last scheduled event for each venue, so that I can determine how the next event should be arranged: first of all, it can not be arranged in the existing venues, I use a ring for (j=1;j<=num;j++) {if (S[i]>=f[a[j]]) {a[j]=i;break;}} To complete this task, I have NUM conference room so I Loop num times, and then

And I said, "Num" is the last sequence of scheduled activities, so just compare the start time of the scheduled activity S "I" and the end time of this "last scheduled activity" S "a" J "" Good, Once the two are found to be compatible, the site is scheduled and the so-called last scheduled activity is updated to I, and the judgment is immediately concluded; otherwise, a new venue will be arranged, and the time of the activity is updated.

O. The following is the corresponding C code.

#include "stdio.h"
int main () {
    int n,i,j,k,num;
    scanf ("%d", &n);
    int s[n+1],f[n+1],a[n+1];
    for (i=1;i<=n;i++) {scanf ("%d%d", &s[i],&f[i]); a[i]=0;}
    A[1]=1;num=1;
    for (i=2;i<=n;i++) {for
        (j=1;j<=num;j++) {
            if (S[i]>=f[a[j]]) {a[j]=i;break;}
        }    
        if (j>num) {num++;a[num]=i;}
    }    
    for (k=1;k<=num;k++) printf ("%d", a[k]);
    printf ("\ n");
    printf ("%d\n", num);
    System ("pause");
    return 0;
}        


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.