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;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值