henuoj 1172 Sanzo’Machine
题目描述:
Sanzo 手上有 n 个任务,每个任务都有起始和结束时间。两个重叠的任务不能使用同一台 Machine 。现在他来请教你这 n 个任务最少需要多少 Machine 呢?
第一行输入一个整数n(1 <= n <= 1e5)
接下来 n 行每行两个数 l , r (1 <= l <= r <= 1e5)表示任务的起始时间和终止时间
样例输入
3
1 4
4 7
2 6
输出
2
分析问题发现,l , r区间内的每一个时间点都不能重复,一旦有重复表示需要多一个机器,而最终需要的最少机器数量,则是重复次数最多的时间点,因此我们可以用差分区间来求解
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 1e5 + 10;
int n;
int ans[maxn];
int main()
{
memset(ans, 0, sizeof ans);
scanf("%d", &n);
for (int i = 0; i < n; i++){
int a, b;
scanf("%d %d", &a, &b);
ans[a]++;
ans[b]--;
}
int res = 0;
int sum = 0;
for (int i = 0; i < maxn; i++){
sum += ans[i];
res = max(res, sum);
}
printf("%d\n", res);
return 0;
}
本文来自博客园,作者:correct,转载请注明原文链接:https://www.cnblogs.com/correct/p/12862092.html

浙公网安备 33010602011771号