题目链接:http://hihocoder.com/problemset/problem/1309
-
5 1 10 2 7 6 9 3 4 7 10
样例输出
-
3
描述
给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, EN ), 计算最少需要多少台机器才能按时完成所有任务。
同一时间一台机器上最多进行一项任务,并且一项任务必须从头到尾保持在一台机器上进行。任务切换不需要时间。
输入
第一行一个整数 N,(1 ≤ N ≤ 100000),表示任务的数目。 以下 N 行每行两个整数 Si, Ei,(0 ≤ Si < Ei ≤ 1000000000),表示任务的起至时间。
输出
输出一个整数,表示最少的机器数目。
思路:相当于求出最大重叠次数,就相当于codeforces 612D ,不过这里的区间很大,需要离散化。
注意:这里的区间是开区间,需要当左端点和右端点相等的时候,右端点先在前面。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
typedef pair<int,int> P;
P a[maxn];
vector<P>vec;
int temp[maxn];
int n;
int main()
{
while( ~ scanf("%d",&n))
{
vec.clear();
int len = 0;
for(int i = 1;i <= n;i ++)
{
scanf("%d%d",&a[i].first,&a[i].second);
temp[len ++] = a[i].first;temp[len ++] = a[i].second;
}
sort(temp,temp + len);
len = unique(temp,temp + len) - temp;
for(int i = 1; i <= n; i ++)
{
int x = lower_bound(temp,temp + len,a[i].first) - temp;
int y = lower_bound(temp,temp + len,a[i].second) - temp;
// cout << x << " " << y << " " << temp[x] << " " << temp[y] << endl;
vec.push_back(P(x,1));vec.push_back(P(y,-1));
}
sort(vec.begin(),vec.end());
int cnt = 0;
int ans = 0;
for(int i = 0; i < vec.size(); i ++)
{
if(vec[i].second == 1)
{
cnt ++;
// cout << temp[vec[i].first] << " " << cnt << endl;
ans = max(ans,cnt);
}
else
{
cnt --;
}
}
printf("%d\n",ans);
}
return 0;
}