您的位置:首页 > 其它

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

2017-08-24 20:50 330 查看
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
;

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: