[PAT Grade A Spring 2021] 7-2 Lab Access Scheduling (25 points)

Nowadays, we have to keep a safe social distance to stop the spread of virus due to the COVID-19 outbreak. Consequently, the access to a national lab is highly restricted. Everyone has to submit a request for lab use in advance and is only allowed to enter after the request has been approved. Now given all the personal requests for the next day, you are supposed to make a feasible plan with the maximum possible number of requests approved. It is required that at most one person can stay in the lab at any particular time.

Input Specification:

Each input file contains one test case. Each case starts with a positive integer N (=2?0?3?? garbled, so embarrassing~ it seems to be no more than 2000 ), the number of lab access requests. Then N lines follow, each gives a request in the format:

hh:mm:ss    hh:mm:ss  where  hh:mm:ss  represents the time point in a day by hour:minute:second, with the earliest time being 00:00:00 and the latest 23:59:59. For each request, the two time points are the requested entrance and exit time, respectively. It is guaranteed that the exit time is after the entrance time.

Note that all times will be within a single day. Times are recorded using a 24-hour clock.

Output Specification:

The output is supposed to give the total number of requests approved in your plan.

Sample Input:

7
18:00:01 23:07:01
04:09:59 11:30:08
11:35:50 13:00:00
23:45:00 23:55:50
13:00:00 17:11:22
06:30:50 11:42:01
17:30:00 23:50:00

Sample Output:

5

Hint:

All the requests can be approved except the last two.

Title:

Is to give the number of activities, and then enter the start time and end time of each activity. There are 24 hours a day, ask how many activities can be arranged at most~

It's so ridiculous, I didn't even see where the event was held. Actually Now given all the personal requests for the next day, you are supposed to make a feasible plan with the maximum possible number of requests approved. It is required that at most one person can stay in the lab at any particular time. This sentence is the most important.

Analysis: [Greedy Algorithm]

I think this question is the simplest, or quite satisfactory~ I learned the algorithm of the activity arrangement problem last semester, which is the same as this one. (So ​​it's really an after-school question hahaha

What is greedy? The greedy strategy is to select the activities that end the earliest one by one.

So first sort the activities according to the end of the activity sooner or later, and then add the first activity after sorting (the earliest end activity among all the activities) into the book array.

The book array here stores the selected activities. Then compare the start time of the next activity with the end time of the last selected activity, and if there is no conflict, add it to the book array. Cycle the entire activity until the last activity.

Since all activities have been sorted in advance according to the end time, the activities stored in the book array are also sorted in chronological order.

Finally, the number of elements in the book array is the maximum number of activities that can be arranged in a day, Bingo!

Code:

#include<iostream>
#include<algorithm>
using namespace std;
struct plan{
	string start,end;
}p[2001];
int cmp(plan a,plan b){
	return a.end<b.end;
}
int book[2001]={0};
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>p[i].start>>p[i].end;
	}
	sort(p,p+n,cmp);
    book[0]=0;    //选中第1个活动 可以没有这个哦,因为已经初始化为0了,为了更方便理解,把这个加上了。
	int j=0;
	for(int i=1;i<n;i++){
		if(p[i].start>=p[book[j]].end)
			book[++j]=i;
	}
	printf("%d",j+1);	
	return 0;
}

Really easy~ Every sentence of my code for this question is not nonsense, although every sentence of other inscriptions is nonsense (I can't laugh or cry...

The food is not ashamed. What’s ashamed is that I didn’t expect that the big guy sitting next to my profession was the first one in our profession. I...I’m really dead in society. Fortunately, he left early (cover his face)

Guess you like

Origin blog.csdn.net/WKX_5/article/details/114755296