题干:
假设要用很多个教室对一组活动进行调度。我们希望使用尽可能少的教室来调度所有的活动。
输入要求:
第一行为活动的个数 N(1<=N<=1 000 000) 。
接下来 N 行为 Si 和 Fi(0<=Si<Fi<=2 000 000 000) ,分别代表第 i 个活动的开始时间和结束时间。活动 i 的区间段为 [Si,Fi)
输出要求:
输出有一行 M ,为所需教室的最小数量
测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
---|---|---|---|---|---|
测试用例 1 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
代码如下:
好经典的贪心。。。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int m = 1e6 + 5;
int Left[m], Right[m];
bool cmp(int a, int b)
{
return a < b;
}
int main()
{
int n, ans = 0;
cin >> n;
for (int i = 0; i < n; i++)
scanf("%d %d", &Left[i], &Right[i]);
sort(Left, Left + n, cmp);
sort(Right, Right + n, cmp);
int cal = 0;
for (int i = 0, j = 0; i < n;)
{
if (Left[i] < Right[j])
{
cal++;
i++;
}
else
{
cal--;
j++;
}
if (ans < cal)
ans = cal;
}
cout << ans << endl;
return 0;
}