zoukankan      html  css  js  c++  java
  • 【贪心】POJ3190: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.
     

    大致题意:

    有一群牛,每个牛都有固定的吃饭时间,并且一个牛槽只能同时让一只牛吃饭。给出所有牛吃饭的开始时间和结束时间,求出最优的策略,使用最少的牛槽让所有的牛吃上饭。输出最少的牛槽数,以及每个牛在第几个牛槽吃。

    大体思路:

    贪心。用到了优先队列。

    1)将牛按照开始时间由小到大排序,开始时间相等的话按结束时间由小到大排序。

    2)创建一个优先队列,在队列中牛按照结束的时间从小到大排序。

    3)把第一只牛加入队列,然后检查第二只牛能否和它同用一槽。如果能,第一只牛出队列,第二只牛进队列。若不能,第二只牛进队列,牛槽数加一。继续该步骤,知道所有的牛都吃过了。

    4)注意,记录牛的数据时,除了开始和结束时间,还要记录牛一开始的顺序,以便记录它在哪个槽吃。

    代码如下:

     1 #include <iostream>
     2 #include <queue>
     3 #include <algorithm>
     4 #include <cstdio>
     5 using namespace std;
     6 const int maxn = 50010;
     7 struct node
     8 {
     9     int st,en,pos;
    10     friend const bool operator < (const node a ,const node b)
    11     {
    12         if(a.en==b.en)
    13             return a.st>b.st;
    14         return a.en>b.en;
    15     }
    16 };
    17 node cow[maxn];
    18 bool cmp(node a,node b)
    19 {
    20     if(a.st==b.st)
    21         return a.en<b.en;
    22     return a.st<b.st;
    23 }
    24 priority_queue<node> q;
    25 int ans[maxn];
    26 int n;
    27 int main()
    28 {
    29     while(~scanf("%d",&n))
    30     {
    31         for(int i=1;i<=n;i++)
    32         {
    33             scanf("%d%d",&cow[i].st,&cow[i].en);
    34             cow[i].pos=i;
    35         }
    36         sort(cow+1,cow+1+n,cmp);
    37         q.push(cow[1]);
    38         int cnt=1;
    39         ans[cow[1].pos]=cnt;
    40         for(int i=2;i<=n;i++)
    41         {
    42             if(q.top().en<cow[i].st)
    43             {
    44                 ans[cow[i].pos]=ans[q.top().pos];
    45                 q.pop();
    46             }
    47             else
    48             {
    49                 cnt++;
    50                 ans[cow[i].pos]=cnt;
    51             }
    52             q.push(cow[i]);
    53         }
    54         printf("%d
    ",cnt);
    55         for(int i=1;i<=n;i++)
    56         {
    57             printf("%d
    ",ans[i]);
    58         }
    59     }
    60     return 0;
    61 }
    View Code
  • 相关阅读:
    执行游戏时出现0xc000007b错误的解决方法
    html中#include file的使用方法
    使用ServletFileUpload实现上传
    java环境变量配置
    微软之左旋转字符串
    Ubuntu下很给力的下载工具
    hibernate官方新手教程 (转载)
    mssql数据库游标批量改动符合条件的记录
    PageRank算法
    Myeclipse7.5 下载 安装 注冊 注冊码 100%成功
  • 原文地址:https://www.cnblogs.com/SoulSecret/p/8454897.html
Copyright © 2011-2022 走看看