hdu 6180 Schedule 贪心

来源:互联网 发布:院士 知乎 编辑:程序博客网 时间:2024/06/05 01:51

题目:

http://acm.hdu.edu.cn/showproblem.php?pid=6180

题意:

有n个任务,每个任务有一个开始时间s_i和结束时间e_i,在一些机器上执行任务,每个机器在一段时间间隔内只能执行一个任务,也就是允许时间点是重合的,在一个机器上执行的两个任务之间有段空闲时间的话,机器不能关闭。求最少需要多少台机器,然后求出在这些机器下执行完所以任务需要的最少时间

思路:

贪心选取,使空闲时间最小。如果当期任务跟其他任务都冲突,就增加一台机器

#include<bits/stdc++.h>using namespace std;typedef long long ll;const int N = 100000 + 10;struct node{    int x, y;} a[N];bool cmp(node a,node b){    return a.x<b.x;}int main(){    int t, n;    scanf("%d", &t);    while(t--)    {        scanf("%d",&n);        multiset<int>ste;        for(int i = 1; i <= n; i++) scanf("%d%d",&a[i].x,&a[i].y);        sort(a + 1, a + 1 + n, cmp);        ll ans = 0;        for(int i = 1; i <= n; i++)        {            auto p = ste.upper_bound(a[i].x);            if(p == ste.begin())//跟之前的所有任务都冲突            {                ans += a[i].y - a[i].x;                ste.insert(a[i].y);            }            else            {                p--;                ans += a[i].y - *p;                ste.erase(p);                ste.insert(a[i].y);            }        }        printf("%d %I64d\n", ste.size(), ans);    }    return 0;}
原创粉丝点击