【问题描述】陈老师是一个比赛队的主教练,有一天,他想给团队成员开会,应该为这次会 议安排教室,但教室缺乏,所以教室管理员必须通过接受订单和拒绝订单优化教室的利用率。 如果接受一个订单,则该订单 的开始时间和结束时间成为一个活动。注意,每个时间段只 能安排一个订单。请找出一个最大化的总活动时间的方法。你的任务是这样的:读入订单, 计算所有活动(接受的订单)占用时间的最大值。
输入描述:标准等的输入将包含多个测试用例。对于每个测试用例,第 1 行是一个整数 n(n<=10 000),接着的 n 行中每一行包括两个整数 p 和 k(1<=p<=k<=300 000),其中 p 是一 个订单的开始时间,k 是结束时间。
输出描述:对于每个测试用例,输出所有活动占用时间的最大值。
输入样例:
4
1 2
3 5
1 4
4 5
样例输出:
4
#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 20 //仅说明方法,未按照题目要求
int n;
int ans, temp, counter;
struct classroom
{
int start;
int end;
};
void dfs(classroom a[], int i)
{
counter = 0;
for (int j = 1; j <= n; j++)
{
if (a[j].start >= a[i].end)
{
int t=(a[j].end - a[j].start);
temp += t;
dfs(a, j);
temp -=t; //回溯
counter++;
}
}
if (counter == 0)
if (temp>ans)ans = temp;
}
int main()
{
cin >> n;
classroom a[MAX];
a[0].end = 0;
for (int i = 1; i <= n; i++)
cin >> a[i].start >> a[i].end;
dfs(a, 0);
cout << ans<<endl;
return 0;
}