zoukankan      html  css  js  c++  java
  • poj 3190 Stall Reservations (贪心+优先队列)

    Description

    Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows. 
    
    Help FJ by determining:
    The minimum number of stalls required in the barn so that each cow can have her private milking period
    An assignment of cows to these stalls over time
    Many answers are correct for each test dataset; a program will grade your answer.

    Input

    Line 1: A single integer, N 
    
    Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

    Output

    Line 1: The minimum number of stalls the barn must have. 
    
    Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

    Sample Input

    5
    1 10
    2 4
    3 6
    5 8
    4 7

    Sample Output

    4
    1
    2
    3
    2
    4

    Hint

    Explanation of the sample: 

    Here's a graphical schedule for this output: 

    Time     1  2  3  4  5  6  7  8  9 10
    
    Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>
    Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..
    Stall 3 .. .. c3>>>>>>>>> .. .. .. ..
    Stall 4 .. .. .. c5>>>>>>>>> .. .. ..
    Other outputs using the same number of stalls are possible.

    Source

     
    一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作。给你每头奶牛的指定时间的区间,问你最小需要多少机器。
     
    先按起始时间从小到大排序,起始时间相同则按结束时间从小到大排序
     
    然后用一个优先队列来维护,vis记录奶牛的工作机器
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<set>
     5 #include<map>
     6 #include<algorithm>
     7 #include<queue>
     8 using namespace std;
     9 #define N 60000
    10 int n;
    11 int vis[N];
    12 struct Node
    13 {
    14     int l,r;
    15     int pos;
    16     bool friend operator < (Node a,Node b)
    17     {
    18         if(a.r!=b.r)
    19             return a.r>b.r;
    20     }
    21 }p[N];
    22 bool cmp(Node a,Node b)
    23 {
    24     if(a.l!=b.l)
    25         return a.l<b.l;
    26     return a.r<b.r;
    27 }
    28 int main()
    29 {
    30     while(scanf("%d",&n)==1)
    31     {
    32         memset(vis,0,sizeof(vis));
    33         for(int i=0;i<n;i++)
    34         {
    35             scanf("%d%d",&p[i].l,&p[i].r);
    36             p[i].pos=i;
    37         }
    38         sort(p,p+n,cmp);
    39 
    40         int ans=1;
    41         priority_queue<Node>q;
    42         q.push(p[0]);
    43         vis[p[0].pos]=ans;
    44 
    45         for(int i=1;i<n;i++)
    46         {
    47             Node tmp=q.top();
    48             if(tmp.r<p[i].l){
    49                 q.pop();
    50                 vis[p[i].pos]=vis[tmp.pos];
    51             }
    52             else{
    53                 ans++;
    54                 vis[p[i].pos]=ans;
    55             }
    56             q.push(p[i]);
    57         }
    58         printf("%d
    ",ans);
    59         for(int i=0;i<n;i++) printf("%d
    ",vis[i]);
    60 
    61     }
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    2021上半年下午第二题
    21年软件设计师上半年下午试题一
    软考下午第三题-用例图和类图
    类图
    用例图-包含、扩展、泛化
    软考下午题二------数据库设计
    软件设计师下午题-数据流图
    IP练习题
    2020软件工程作业02
    2020软件工程作业01
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4764549.html
Copyright © 2011-2022 走看看