G. Car Repair Shop(优先队列)

来源:互联网 发布:淘宝退款原因质量问题 编辑:程序博客网 时间:2024/06/06 01:35

2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest
G. Car Repair Shop

题意:修汽n辆车,每次只能修一辆,每辆车有要求开始修理的时间si,修理需要的时间是di。现在安排修车计划,能在要求时间si修的车按照si时间修,否则便尽量往前安排,时间从1开始。

题解: 维护起始点递增的时间区间,不断加入即可。
其实同A题一样,可以用优先队列维护。

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>#include <stack>using namespace std;const int N = 200+10;typedef long long LL;const int INF=0x3f3f3f3f;struct Node{    int l ,r;}a[N],now;bool pan(Node a,Node b){    if(a.l > b.r || a.r < b.l)        return true;    return false;}bool cmp(Node a, Node b){    return a.l < b.l;}int main(){    int n;    scanf("%d",&n);    int i ,j;    for(i = 0; i < n; i++)    {        int x,y;        scanf("%d%d",&x,&y);        if(i == 0)        {            a[i].l = x;            a[i].r = x+y-1;            printf("%d %d\n",a[i].l,a[i].r);        }        else        {            now.l = x;            now.r = x+y-1;            sort(a,a+i,cmp);            for(j = 0; j < i; j++)            {                if(!pan(now,a[j]))                    break;            }            if(j == i)            {                a[i] = now;                printf("%d %d\n",a[i].l,a[i].r);                continue;            }            now.l = 1;            now.r = y;            for(j = 0; j < i; j++)            {                if(pan(now,a[j]))                {                    a[i] = now;                    printf("%d %d\n",a[i].l,a[i].r);                    break;                }                else                {                    now.l = a[j].r+1;                    now.r = a[j].r+y;                }            }            if(j == i)            {                a[i] = now;                printf("%d %d\n",a[i].l,a[i].r);            }        }    }    return 0;}
0 0
原创粉丝点击