会场问题 差分解法

之前写会场问题都是用贪心,很麻烦。(给出开始结束时间,求最少需要房间数)
差分写法如下:

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
typedef unsigned long long ULL;
using namespace std;

bool Sqrt(LL n) { return (LL)sqrt(n) * sqrt(n) == n; }
const double PI = acos(-1.0), ESP = 1e-10;
const LL INF = 99999999999999;
const int inf = 999999999, N = 2e6 + 24;
int n, a, b, u, h[N];

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d%d", &a, &b);
		h[a]++; h[b+1]--;
		if(b > u) u = b;
	}
	a = h[0];
	for(int i = 1; i <= u; i++) {
		h[i] += h[i-1];
		if(h[i] > a) a = h[i];
	}
	printf("%d
", a);


	return 0;
}
/*
    input:
    output:
    modeling:
    methods:
    complexity:
    summary:
*/
/*
    input:
    output:
    modeling:
    methods:
    complexity:
    summary:
*/
原文地址:https://www.cnblogs.com/000what/p/11656869.html