"Greedy algorithm" dance room arrangement

Source: Internet
Author: User

Description

New Live has a dance room, and only a dance room, assuming that the application time in hours, 24 hours a day, 168 hours a week, we stipulate that the application time from 0 points per Monday increments, such as the application time interval of "1,24" represents Monday 0 to 24, time interval "25,48" Represents 0 to 24 points in Tuesday, and so on.

Now assume that you are the management of the dance room, in the face of a group of applications to use the dance room, you want the dance room to be as much as possible to the use of more people (that is, as much as possible to meet the number of applications, please note that this is not required in the dance room to use the most, but the number of approved applications Please write a program to determine how many of these applications can be satisfied. For example, the following two applications are available:

"10,12"

"11,160"

Since the same time the dance room can only be used for one application, so the two applications can only meet the maximum of 1.

The following three applications are also as follows:

"10,12"

"155,168"

"11,160"

Since the requirement is to allow more applications to be approved, we have chosen "10,12" and "155,168", instead of the "11,160" interval, so the result is 2.

Input

The first line is a number n, which represents the next n test cases.

For each test case, the format is as follows:

The first line is a number m, representing a m application for this week, 0 < M < 150.

The next M-line, each line has two numbers, the middle is separated by a space, representing the time interval of the application (note that the time interval is closed interval, and the time interval must be legal, will not exceed "1,168")

Output

Each test case output is a single line with only one number representing the maximum number of applications that can be satisfied.

For each of these n test cases, the output has a total of n rows

Sample Input
2210 1211 160310 12155 16811 160
Sample Output
12

Problem Solving Ideas:
The choice of greedy algorithm does not intersect the interval problem.
1. A structure is used to indicate the interval, and the interval is sorted by the end time;
2. It is possible to arrange (and not to have a conflict with) the first to be completed so that the activities of the Organization are as much.

Implementation code:

#include <iostream>usingstd::cout;usingstd::cin;usingStd::endl;structTime {intb, E;};intMain () {intN, M, I, J, Count; Time t[ Max];    Time TMP; CIN>>N;  for(; n >0; n--) {//n A test exampleCIN >>m;  for(j =0; J < M; J + +) {cin>>t[j].b; CIN>>T[J].E; }                 for(j =0; J < M-1; J + +)             for(i =0; I < M-1-J; i++) {                if(T[I].E >= T[i +1].E) {//Sort by end time in ascending orderTMP =T[i]; T[i]= T[i +1]; T[i+1] =tmp; }            }                     for(i =0, Count =0; I < m; i++) {            if(i = =0) {Count++; TMP=T[i]; Continue; }            if(T[i].b > TMP.E) {//The next start time is greater than the last scheduled end timeTMP = T[i];//This activity can be arranged .count++; }} cout<< Count <<Endl; }}            

(Ben Boven more or less reference other online materials, but the time has long forgotten the original reference, here to thank them! )

"Greedy algorithm" dance room arrangement

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.