原题:https://hihocoder.com/problemset/problem/1309
一道区间的题目,画个图就可以知道,最少的机器数目=被最多运行区间覆盖的数目。只要维护一个记录当前最大区间覆盖数目就要,重合边界点要注意,起点要在终点的前面。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//记录点,true为一个区间的起点,false为一个区间的终点
struct point{
int x;
bool flag;
};
bool cmp(const point& a,const point& b)
{
if(a.x!=b.x)
{
return a.x<b.x;
}else{//边界点重合的话,新区间的起点要在旧区间的终点前面
return b.flag==true;
}
}
int main()
{
int n;
vector<point> pos;
while(cin>>n)
{
for(int i=0;i<n;i++)//读取
{
point begin,end;
cin>>begin.x>>end.x;
begin.flag=true;
end.flag=false;
pos.push_back(begin);
pos.push_back(end);
}
//按规则排序
sort(pos.begin(),pos.end(),cmp);
int cnt=0;
int ans=0;
//统计区间覆盖数
for(int i=0;i<pos.size();i++)
{
if(pos[i].flag==true){
cnt++;
}else{
cnt--;
}
//更新
ans=max(ans,cnt);
}
cout<<ans;
}
return 0;
}