Task Distribution--HihoCoder-1309

Given N tasks with starting time and end time ( S1E1 ), ( S2E2 ), ..., ( SNEN), try to distribute and finish them with the minimum number of machines.

At the same time there is at most one running task on each machine and one task should keep running on one machine.

Input

The first line contains 1 integer N (1 ≤ N ≤ 100000), the number of tasks.

The following N lines each contain 2 integers Si and Ei,(0 ≤ Si < Ei ≤ 1000000000), the starting and end time of i-th task.

Output

The minimum number of machine needed.

Sample Input
5
1 10
2 7
6 9
3 4
7 10
Sample Output
3
 
 
题意:给n个的任务开始和结束时间,然后让你给这些任务分配执行任务的机器,每个机器在一段时间只能执行一个任务,问最少要分配多少机器。
 
 
思路:思路来源于挑战40页的区间问题,那个问题按照结束时间排序,求得是最多能接任务的数量,认真分析本题,可以想到:每个任务都会被分配机器去完成,所以可以按任务开始时间从小到大排序;用优先队列来维护当前机器正在做的任务;优先队列按照任务结束时间从小到大排序,结束最早就最早出队,队列最终尺寸就是答案。
 
 
 
 
#include<bits/stdc++.h>
using namespace std;
#define maxn 1000005
struct node
{
    int s;
    int t;
   bool operator <(const node&a)const
    {
        return a.t<t;
    }
}a[maxn];
bool cmp(node a,node b)
{
    return a.s==b.s?a.t<b.t:a.s<b.s;
}
priority_queue <node> p;
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i].s>>a[i].t;
    sort(a,a+n,cmp);
    p.push(a[0]);
    int sum=1;
    for(int i=1;i<n;i++)
    {
           if(p.top().t<=a[i].s)
           {p.pop();}
        p.push(a[i]);
    }
    cout<<p.size()<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值