任务分配hihocoder 1309(离散化 )


题目链接:http://hihocoder.com/problemset/problem/1309

描述

给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, EN ), 计算最少需要多少台机器才能按时完成所有任务。

同一时间一台机器上最多进行一项任务,并且一项任务必须从头到尾保持在一台机器上进行。任务切换不需要时间。

输入

第一行一个整数 N,(1 ≤ N ≤ 100000),表示任务的数目。 以下 N 行每行两个整数 Si, Ei,(0 ≤ Si < Ei ≤ 1000000000),表示任务的起至时间。

输出

输出一个整数,表示最少的机器数目。

样例输入
5
1 10
2 7
6 9
3 4
7 10
样例输出
3

思路:相当于求出最大重叠次数,就相当于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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值