POJ 3190 Stall Reservations

来源:互联网 发布:macd指标源码 编辑:程序博客网 时间:2024/05/17 04:10

题意:

  说几头牛,每头牛挤牛奶的时间是分别是Ai到Bi,一个机器不能在相同的时间给超过一个牛挤牛奶,问最少用几个机器,和每个牛奶所用的机器编号。

思路:

  首先,用每头牛开始的时间Ai进行一个排序,然后再用优先队列将最小的Bi为优先,如果最小Bi都比这个Ax大,则会重合,增加一个机器,否则用同一个机器。

代码:

#include<stdio.h>#include<queue>#include<algorithm>using namespace std;struct Node{    int A,B;    int pre;    bool operator <(const Node &c)const    {        if(B==c.B)            return A>c.A;        return B>c.B;    }}x[50010];priority_queue<struct Node >q;int cmp(Node a,Node b){    return a.A<b.A;}int main(){    int N,d[50010];    scanf("%d",&N);    for(int i=0;i<N;i++)    {        x[i].pre=i;        scanf("%d %d",&x[i].A,&x[i].B);    }    sort(x,x+N,cmp);    d[x[0].pre]=1;    q.push(x[0]);    int ans=1;    for(int i=1;i<N;i++)    {        if(q.top().B<x[i].A)        {            d[x[i].pre]=d[q.top().pre];            q.pop();        }        else        {            ans++;            d[x[i].pre]=ans;        }        q.push(x[i]);    }    printf("%d\n",ans);    for(int i=0;i<N;i++)        printf("%d\n",d[i]);    return 0;}


  

0 0
原创粉丝点击