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
  • 相关阅读:
    Redis数据类型有哪些?
    python---判断元素是否可用 is_displayed/is_enabled/is_selected
    pythone---获取文本链接,text/get_attribute/current_url/title
    python---清空 clear
    python---获取元素 CSS
    JDK安装及配hi环境
    python---获取元素 Xpath
    python---获取元素 id/name/class_name/link_text/partial选择器及注意事项
    python---数据类型----set集合和字典
    web搭建自动化环境
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4764549.html
Copyright © 2011-2022 走看看