Other practice questions (uncategorized)

Description The
school's small auditorium has many activities every day. When there is time, the planned time of these activities will conflict, and some activities need to be selected for holding.
Xiao Liu's job is to arrange activities in the school's small auditorium, at most one activity can be arranged at each time. Now Xiao Liu has a schedule of some activity plans.
He wants to arrange as many activities as possible. May I ask him how to arrange it.

Input The
first line is an integer number m (m<100) which means there are m groups of test data.
The first line of each group of test data is an integer n (1<n<10000), which means that there are n activities in the test data.
The following n rows, each row has two positive integers Bi, Ei (0<=Bi,Ei<10000), which respectively represent the start and end time of the i-th activity (Bi<=Ei)

Output
For each group of inputs, output the maximum number of activities that can be scheduled.
The output of each group occupies one line

Sample input
2
1 10
10 11

3
1 10
10 11
11 20

Sample output
1
2

Thought point:
This problem is more convenient to solve by applying sort in STL. Sort in ascending order according to the time when the activity ends, and then judge the start time of the next activity and the end time of the previous activity, and compare it, if the difference is greater than 1, then the count is increased by 1.

#include<iostream>
#include<algorithm>
using namespace std;

struct meet
{
    
    
	int start;
	int end;
}a[100010];

bool cmp(meet a, meet b)               //按会议结束时间排列                 
{
    
    
	return a.end<b.end;
}

int main()
{
    
    
	int n;
	cin>>n;
	for (int i = 1; i <= n; i++)
	{
    
    
		cin >> a[i].start;
		cin >> a[i].end;
	}
	a[0].start = -1;
	a[0].end = -1;
	sort(a + 1, a + n + 1, cmp);  //排列
	int sum = 1;
	int j = 1;
	for (int i = 2; i <= n; i++)  //从第二个会场开始从与前一个会场比较
	{
    
    
		if (a[i].start>a[j].end)  //如果相隔时间大于1(输入都为整数),则活动数+1
		{
    
    
			j = i;
			sum++;
		}
	}
	cout << sum << endl;

	system("pause");
	return 0;
}

Post-code reflection: The
key point is still the conversion of the problem, and the problem must be converted reasonably, so that the problem is much easier to solve.

Guess you like

Origin blog.csdn.net/qq_27538633/article/details/105911595