hiho任务分配

原题: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值