lightoj1301Monitoring Processes

思路:有n个进程,开始时间s[i],结束时间t[i],我们要用监视器来监视这些进程,但是每个监视器同一时间只能监视一个进程,只有当这个监视器监视的进程结束后才能监视下一个,问需最少要多少个监视器才能监视完所有进程。

这题也是贪心,n个进程有2n个时间点,一个时间点要么是一个进程的开始,要么是一个进程的结束,开始的话就需要派监视器来监管,这个监视器有两个来源,一是前面已经监视完别的进程而空下来的,再就是额外添加的,所以我们需要一个pre来记录到目前为止需要多少个,ans表示到目前为止添加了多少个。存在同个时间是某些进程的开始,也是某些进程的结束,由于同一时间不能监视两个,所以要先算开始的,再算结束的。开始就需要的++,结束的就需要的--,再与已有的进行大小比较取最大就好了。

// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
// #define DEBUG
#ifdef DEBUG
#define debug(...) printf( __VA_ARGS__ )
#else
#define debug(...)
#endif
#define CLR(x) memset(x, 0,sizeof x)
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
ii A[100010];
int main()
{	
	freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t, n, icase = 0;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		int top = 0;
		for (int i = 1, x, y;i <= n;++i){
			scanf("%d%d",&x,&y);
			A[top++] = ii(x,-1);
			A[top++] = ii(y, 1);
		}
		sort(A,A+top);
		int ans = 0, pre = 0;
		for (int i = 0;i < top;++i){
			pre -= A[i].second;
			if (pre > ans) ans = pre;
		}
		printf("Case %d: %d\n", ++icase, ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值