【多校训练】hdu 6180 Schedule 贪心+set

Problem Description
There are N schedules, the i-th schedule has start time  si  and end time  ei  (1 <= i <= N). There are some machines. Each two overlapping schedules cannot be performed in the same machine. For each machine the working time is defined as the difference between  timeend  and  timestart  , where time_{end} is time to turn off the machine and  timestart  is time to turn on the machine. We assume that the machine cannot be turned off between the  timestart  and the  timeend
Print the minimum number K of the machines for performing all schedules, and when only uses K machines, print the minimum sum of all working times.
 

Input
The first line contains an integer T (1 <= T <= 100), the number of test cases. Each case begins with a line containing one integer N (0 < N <= 100000). Each of the next N lines contains two integers  si  and  ei   (0<=si<ei<=1e9) .
 

Output
For each test case, print the minimum possible number of machines and the minimum sum of all working times.
 

Sample Input
  
  
1 3 1 3 4 6 2 5
 

Sample Output
  
  
2 8

题意:

给出n个任务,每个任务工作时间位s到e。每台机器不能同时运行多个任务。求最少的机器台数,及当前机器下的最小总用时。

思路:

比较经典的贪心问题,对任务按开始时间排序。选择结束时间小于且最接近当前任务开始时间的机器完成当前任务,如果找不到,就新开一个机器来完成当前任务。

需要用到set来维护机器列表,这里用到了一些基本的set操作。


//
//  main.cpp
//  1010
//
//  Created by zc on 2017/8/24.
//  Copyright © 2017年 zc. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#define ll long long
using namespace std;
const int N=110000;
struct node
{
    ll s,e;
}a[N];

int n;

bool cmp(node a,node b)
{
    return a.s<b.s;
}
multiset<ll> s;
int main(int argc, const char * argv[]) {
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%lld%lld",&a[i].s,&a[i].e);
        sort(a,a+n,cmp);
        s.clear();
        ll ans=0;
        for(int i=0;i<n;i++)
        {
            auto it=s.upper_bound(a[i].s);
            if(it==s.begin())
            {
                ans+=a[i].e-a[i].s;
                s.insert(a[i].e);
            }
            else
            {
                it--;
                ans+=a[i].e-*it;
                s.erase(it);
                s.insert(a[i].e);
            }
        }
        printf("%d %lld\n",s.size(),ans);
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值