【多校训练】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);
}
}
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);
}
}
相关文章推荐
- HDU 6180 && 2017 多校训练:Schedule
- (2017多校训练第五场)HDU - 6092 Rikka with Subset 0-1背包
- HDU 4864 Task (2014多校联合训练第一场1004) 解题报告(贪心)
- hdu 4649 Professor Tian 多校联合训练的题
- 2017 多校训练第一场 HDU 6040 Hints of sd0061
- HDU-2017 多校训练赛10-1001-Admiral
- 【多校训练】hdu 6171 Admiral 双向bfs+hash
- 2017 多校训练第三场 HDU 6060 RXD and dividing
- (2017多校训练第三场)HDU - 6063 RXD and math 找规律 + 快速幂
- HDU 6162 && 2017 多校训练:Ch's gift(树链剖分)
- HDU 4862 Jump(多校联合训练1)(最小费用最大流)
- 【多校训练】 hdu 6092 Rikka with Subset
- HDU 6178 && 2017 多校训练:Monkeys(DFS)
- 【多校训练】 hdu 6033 Add More Zero
- 【2017多校训练2+计算几何+板】HDU 6055 Regular polygon
- hdu 4602 partition 2013多校联合训练第一场
- 2017 多校训练第三场 HDU 6066 RXD's date
- (HDU 5723)Abandoned country <最小生成树 + 树上所有两点之间的距离的期望> 多校训练1
- (HDU 5792)World is Exploding <树状数组+去重> 多校训练5
- hdu 4632 Palindrome subsequence 区间dp 多校联合训练第四场