ACM_Interval scheduling problem (greedy)

Meetings Series One

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

After many years, the Guangcai ACM editorial association is in full swing, with several departments under it, and the members of the editorial association are nearly 100. This time, in order to celebrate the rapid development of the ACM Editors Association in recent years, all departments decided to hold a meeting on the same day. Please note: This time, due to resource constraints, only one lecture hall will be applied for. Now you need to come up with a plan to make as many meetings as possible within 24 hours of the same day.
 In order to simplify the problem, the following provisions are made:
(1) Each meeting M(a, b) starts from time a and ends at time b (where a, b are integers and 8 <= a < b <= 20 );
(2) Only one department is allowed to hold meetings in the lecture hall in the same time period, and the time consumed by different sessions can be ignored when handing over different departments.

Input:

The input contains multiple sets of test data, and an integer n (0<n<=20) is entered in the first line of each set of data, indicating that there are a total of n meetings to be arranged. The next n lines enter the start and end times a and b of each meeting in turn.

Output:

The maximum number of sessions that can be arranged for each group of test outputs, occupying one line.

Sample Input:

3
12 15
12 13
14 18
5
11 12
13 18
18 20
13 16
16 17

Sample Output:

2
4 
Problem-solving ideas: Greedy strategy: Sort all intervals according to the coordinates of the right endpoint (end time) from small to large, and process each interval in sequence. If it does not overlap with all the currently selected intervals, the interval is selected, otherwise it is not selected. Note: The start time and end time are in the range of 8-20.
The algorithm proves to see him: ---> blogger: Sunshine log

AC code:
1 #include<bits/stdc++.h>
 2  using  namespace std;
 3  struct NODE{
 4      int st,ed;
 5 }node[ 21 ];
 6  bool cmp(NODE x,NODE y){
 7      return x.ed<y .ed;   // sort 8 in ascending order with the earliest end time 
}
 9 int main()
 10 {
 11 int n,num,k,t,s,e;
 12 while (cin>> n){
 13          k=- 1 ;
 14 for ( int i=                      1;i<=n;++i){
15             cin>>s>>e;
16             if(s>=8 && e<=20){node[++k].st=s;node[k].ed=e;}
17         }
18         sort(node,node+k+1,cmp);
19         num=t=0;
20         for(int i=0;i<=k;++i)
21             if(t<=node[i].st){num++;t=node[i].ed;}
22         cout<<num<<endl;
23     }
24     return 0;
25 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326091600&siteId=291194637