给出N个活动开始和结束时间[start,end),要问需要最小的容器数装下这些活动?
贪心:把所有的时间放在一个数组中排序,相同时间按照结束时间放在开始时间的左边,碰到开始时间就将计数器+1,结束时间就-1,输出过程中的计数器最大值
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n;
struct Node{
long long time;
bool flag; //left = 0,right = 1
};
vector<Node> arr;
inline bool cmp(const Node &a,const Node &b){
if(a.time < b.time)
return true;
else if(a.time > b.time)
return false;
else if(a.flag == 1 && b.flag == 0)
return true;
else return false;
}
inline void Init(){
scanf("%d",&n);
long long l,r;
for(auto i = 0;i < n; ++i){
scanf("%lld %lld",&l,&r);
Node temp;
temp.time = l;
temp.flag = false;
arr.push_back(temp);
temp.time = r;
temp.flag = true;
arr.push_back(temp);
}
sort(arr.begin(),arr.end(),cmp);
}
int main()
{
int ans = 0,temp = 0;
Init();
for(auto i = 0;i < 2*n;++i){
if(arr[i].flag == false)
temp++;
else
temp--;
if(temp > ans)
ans = temp;
}
printf("%d\n",ans);
return 0;
}