Greedy algorithm--ny 14 venue scheduling issues

Source: Internet
Author: User
Tags current time time limit

1) Topics

Venue arrangement Questions

Time limit: Ms | Memory Limit: 65535 KB

Difficulty: 4

Description

There are many activities in the school's chapel every day, and there are times when these activities are scheduled to conflict and some activities need to be selected. Xiao Liu's job is to arrange the activities of the school chapel, with a maximum of one event per time. Now Xiao Liu has some schedule of activities, he would like to arrange as many activities as possible, ask him how to arrange.

input

The first line is an integer m (m<100) representing a total of M-Group test data.
The first line of each set of test data is an integer n (1<n<10000) that indicates that there are n activities in the test data.
Subsequent n rows, each line having two positive integers bi,ei (0<=bi,ei<10000), representing the start and end time of the first activity (BI<=EI)

Output

For each set of inputs, the output can have a maximum number of activities to schedule.
One row for each set of outputs

Sample Input

2

2

1 10

10 11

3

1 10

10 11

11 20

Sample Output

1

2

Tips

Note: If the previous activity ends at t time, the next activity should start at t+1 time first

 

2) Test Instructions

If the two activities overlap in time, there will be conflict and cannot be carried out simultaneously. Give a set of activity schedules to find out how many activities can be scheduled.


3) data range

The number of test data groups is less than 100;

The number of activities of a set of data is n,1<n<10000, enumeration is not feasible;

The start time for the activity is B and the end time is e,0<=b<=e<=10000.


4) Algorithm

The information for an event has a start time, duration, and end time.

If the start time is preferred, such as when an activity starts at 0 o'clock and ends at 10000, then only one activity can be performed, which is obviously not feasible.

If you prefer a short duration, such as a set of data:

3

1 5

5 6

6 10

The first choice is the second activity, and one and the third cannot be done, so it is not feasible.

If you choose the end time earlier, you can free up as much time as possible for the next activities to use, feasible.

Therefore, the greedy algorithm is used to prioritize activities that have an early end time.


5) Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define SIZE 10005

struct activity_info
{
	int s;	Start time
	int e;	End time
}acts[size];

BOOL Cmp (const activity_info A, const activity_info b)
{
	return A.E < B.E;
}

int arrangingactivities (int n)
{
	//by end time from small to large sorted sort
	(Acts, acts+n, CMP);

	int count = 0;
	int currtime =-1;	Current time
	int i;
	for (i = 0; i < n; i++)
	{
		if (Acts[i].s > Currtime)
		{
			count++;
			Currtime = ACTS[I].E;
		}
	}
	return count;
}

int main (void)
{
	int ncases;
	scanf ("%d", &ncases);
	while (ncases--! = 0)
	{
		int n;
		scanf ("%d", &n);

		int i;
		for (i = 0; i < n; i++)
		{
			scanf ("%d%d", &acts[i].s, &ACTS[I].E);
		}

		printf ("%d\n", Arrangingactivities (n));
	}
	return 0;
}

6) test data

4

2

1 10

10 11

3

1 10

10 11

11 20

6

1 10

12 16

11 17

10 11

2 3

4 8

3

1 5

5 6

6 10


7) Submit Results


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.