区间的最大重叠度(会议安排问题)

其实就是区间最多重叠数目。

 

定义一个op作为区间重叠度,从头扫描到尾部,若op进入一个区间的开始端,说明op在此区间内部,继续扫描若在遇到一个区间的开始端op就再加一,若op遇到一个区间的末端,说明op已经走出某个区间,就让op减一,就这样让op一直走到结束,记录下op的曾经的最大值,就是区间重叠最多的和数目
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 0x7fffffff;
const int maxn = 1000000;
int n;
int v[maxn];
 
 
int main()
{
    int from, to, i, res;
    while(scanf("%d", &n) != EOF) {
        memset(v, 0, sizeof(v));
        for(i = 0; i < n; i++) {
            scanf("%d%d", &from, &to);
            v[from] += 1;
            v[to] += -1;
        }
        res = 0;
        int maxv = -INF;
        for(i = 0; i < maxn; i++) {
            res += v[i];
            if(res > maxv) {
                maxv = res;
            }
        }
        cout << maxv << endl;
    }
    return 0;
}

 

posted @ 2015-07-29 16:46  -梦里不知身是客  阅读(1357)  评论(0编辑  收藏  举报