会场问题 差分解法

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

/**/
#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 = 2000000 + 5;
int n, u, a, b, h[N];

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

    return 0;
}
/*
    input:
    output:
    modeling:
    methods:
    complexity:
    summary:
*/

猜你喜欢

转载自www.cnblogs.com/000what/p/11656869.html