区间的最大重叠度(会议安排问题)
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;
}
定义一个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;
}
相关文章推荐
- A Mini Locomotive(动态规划 01)
- hdu-5750 Dertouzos(数论)
- 01背包 和 完全背包 详解
- Square(强大的剪枝)
- 继续畅通工程(kruskal prim)
- 最小生成树问题(Kruskal 算法)(克鲁斯卡尔)
- Dividing (多重背包 搜索)
- Jack Straws(判断线段是否相交 + 并查集)
- 欧拉函数
- Prime Ring Problem(搜索)
- Cleaning Shifts(区间覆盖)
- Piggy-Bank(完全背包)
- 丑数
- 放苹果(动态规划)
- floyd详解
- 简单版贪吃蛇(广搜+保存路径)(广搜保存路径 超时)
- Fire Net(深搜 和一前不一样的深搜)
- Tangled in Cables(Kruskal+map容器处理字符串)
- 挑战密室(模拟)
- 动态规划 1235: 最大连续子序列