The second greed of the arrangement of activities

Source: Internet
Author: User
There are a number of activities, the start and end time of the i is [SI,FI], the activities can not overlap, to arrange the activities are completed, at least a few classrooms.

Analysis: Can follow the solution of one problem, each classroom arranges as many activities as possible, namely by the end time sort, then greedy chooses the non-conflict activity, arranges a classroom, the remaining activity assigns another classroom, continues the greedy choice ...

Counter example: a:[1,2) b:[1,4) c:[5,6) d:[3,7)

have been sorted by end time, we will choose
Classroom 1: A C
Classroom 2:b
Classroom 3:d
3 classrooms are required.
But if we change the arrangement, we can arrange ad in one classroom, and BC in another classroom, two classrooms is enough.
So the previous greedy strategy couldn't solve the problem.
What to do. The previous strategy is to use a classroom to find all the activities it can arrange, that is, to use the classroom to find activities, we can use activities to find the classroom.
Strategy: Prioritize activities according to the start time, or add a classroom if a conflict occurs.
In a nutshell, the strategy is that we sort the activities in a small order from the start time. Assuming that K classrooms are currently assigned (obviously K is initially equal to 0), for the current activity,
(1) If it can be arranged in one of the K classrooms, arrange it in one of the classrooms, and K will not change.
(2) Otherwise it conflicts with the activities in each classroom, then add a classroom and arrange this activity.
Is this strategy optimal?
Let's think of the process of adding 1 to K: because we sort by the start time, which means that at the beginning of the activity we are considering, there is no end to the activity in K classrooms (because if a classroom activity is over, we can arrange the activity to enter that classroom without conflict, thus not adding k). This means that at the beginning of the event, the activity is currently under consideration, there are (K + 1) Activities in progress, at the same time (k + 1) Activities in progress, no matter how we arrange the classroom, we need at least (k + 1) classroom. Because each classroom cannot carry out two activities at the same time. And our strategy exactly needs (k + 1) classrooms, so it's optimal.
This strategy also tells us that if you consider this issue from the "macro" on the timeline. Consider the number of simultaneous activities at each point in time, as the thickness of this point in time (the start and end time of the activity is imagined as a line segment, so how many segments of each point of time to cover it, can be simply understood as "thickness"), we need at least the maximum thickness of so many classrooms- Because at that time there was a maximum thickness and so many activities at the same time, and our greedy strategy just gave us a plan to arrange all the activities with a maximum thickness of multiple classrooms.
If we only need the number of classrooms, we can sort all the start and end times, meet the start time to add 1 thickness, meet the end time to reduce the thickness of 1, obviously the initial and final end of the thickness is 0, in a series of thickness changes in the process, peak (maximum) is the maximum number of simultaneous activities, And the number of classrooms we need at least.
Finally, we provide input and output data, you write a program, the implementation of this algorithm, only write the correct program, to continue the course behind.
The first line, a positive integer n (n <= 10000), represents the number of activities.
the second line to the (n + 1) row contains n start time and end time. The
start time is strictly less than the end time, and the time is a non-negative integer, less than 1000000000

A row contains an integer that represents the minimum number of classrooms.

Input Example
1 2
3 4
2 9

Sample Output

The above text comes from 51nod, the problem solving code is as follows

#include <stdio.h>
#include <algorithm>
using namespace std;
struct s{
	int s,e;
} S[10002];int l[10002];
BOOL CMP (S x,s y) {
	return x.s<y.s;
int main ()
	int n,ans=0;
	scanf ("%d", &n);
	for (int i=0;i<n;i++)
		scanf ("%d%d", &S[I].S,&S[I].E);
	Sort (s,s+n,cmp);
	for (int i=0;i<n;i++) {
		int t=0;
		while (s[i].s<l[t++]);
		if (T>ans) ans=t;
	printf ("%d\n", ans);
	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: 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.