您的位置:首页 > 其它

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

2016-07-24 00:00 267 查看
其实就是区间最多重叠数目。

定义一个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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: