Data structure and algorithm (application of greedy strategy: event scheduling problem)

Suppose there is a set of n activities E={a1,a2,...,an}, each of which requires the use of the same resource (such as a certain equipment, classroom, venue, etc.), and only one activity is allowed to use it at the same time This resource.
Each activity has a start and end time si,fi that requires the use of the resource, and si<fi. If the activity ai is selected, it occupies resources in the half-open time interval [si, fi). Two activities ai, aj are said to be compatible if and only if their time intervals [si,fi) and [sj,fj) do not intersect, that is, si>=fj or sj>=fi. It is now required to select the largest compatible activity subset in the given activity set. (Hint: Greedy strategy) The
main function has been given, please add the Sort and Select functions. Only two function codes are submitted in the answer area, and other codes are not allowed to be added or modified!
#define Maxn 100
//Define the type of activity
typedef struct act_Node
{int Id; //Activity ID
int s_Time; //Activity start time
int f_Time; //Activity end time
} ACND;
// ******** Sort function**********
// ******** Select function**********
int main()
{ACND arr[Maxn];
int an,i;
cin>>an; //Read in the number of activities
//Read in the number of each activity and the start and end time of the occupied resources
for(i=0;i<an;i++)
cin>>arr[i].Id>>arr[i].s_Time>>arr[i].f_Time;
Sort(an,arr);
Select(an,arr);
return 0;
}
input, there are multiple lines, The first line is the number of activities n, the next n lines, 3 integers in each line, are the number of each activity, the start time and end time of the resource occupied,
output, the selected largest subset of activities, that is, there are multiple rows, Each line includes the number, start time, and end time of the event.
For example:
enter:

11
1 3 8
2 2 13
3 1 4
4 5 7
5 6 10
6 8 11
7 12 14
8 5 9
9 3 5
10 0 6
11 8 12

Output:

3:1-4
4:5-7
6:8-11
7:12-14

Greedy criterion: Arrange the activity earlier, and if the end time is the same, arrange the start time earlier.
The first event must be scheduled first. When the start time of the current event is greater than or equal to the end time of the last scheduled event, the current event is scheduled.

//对活动按贪心准则排序
void Sort(int n,ACND arr[])
{
    
    
    ACND t;
    for(int i=0;i<=n-1;i++)
    {
    
    
        for(int j=i+1;j<=n-1;j++)
        {
    
    
            if(arr[i].f_Time>arr[j].f_Time)
            {
    
    
                t=arr[i];
                arr[i]=arr[j];
                arr[j]=t;
            }
            else if(arr[i].f_Time==arr[i].f_Time)
            {
    
    
                if(arr[i].s_Time>arr[i].s_Time)
                {
    
    
                    t=arr[i];
                    arr[i]=arr[j];
                    arr[j]=t;
                }
            }
        }
    }
}

//进行贪心选择,得到最大相容的活动集合输出
void Select(int n,ACND arr[])
{
    
    
    cout<<arr[0].Id<<":"<<arr[0].s_Time<<"-"<<arr[0].f_Time<<endl;
    int last=arr[0].f_Time;
    for(int i=1;i<=n-1;i++)
    {
    
    
        if(last<=arr[i].s_Time)
        {
    
    
            last=arr[i].f_Time;
            cout<<arr[i].Id<<":"<<arr[i].s_Time<<"-"<<arr[i].f_Time<<endl;
        }
    }
}

Guess you like

Origin blog.csdn.net/upc122/article/details/106668659