zoukankan      html  css  js  c++  java
  • poj 3190 Stall Reservations

    http://poj.org/problem?id=3190

    Stall Reservations
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 3567   Accepted: 1276   Special Judge

    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

    分析:

    这个题是说一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作。给你每头奶牛的指定时间的区间,问你最小需要多少机器。

    最开始想的是以奶牛要求时间的结束点从小到大进行排序,但后来发现这样的想法是错误的。

    1 6
    2 8 8
    3 4 8
    4 6 10
    5 1 3
    6 2 4
    7 4 7

    后来经过调整,应该先按奶牛要求的时间起始点进行从小到大排序,然后维护一个优先队列,里

    面以已经开始挤奶的奶牛的结束时间早为优先。然后每次只需要检查当前是否有奶牛的挤奶工作已经完成的机器即可,若有,则换那台机器进行工作。若没有,则加一台新的机器。

    AC代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<queue>
     6 using namespace std;
     7 const int maxn=60000;
     8 int n,use[maxn];
     9 struct Node
    10 {
    11     int l;
    12     int r;
    13     int pos;
    14     bool operator <(const Node &a)const 
    15     {
    16     if(r==a.r)
    17         return l>a.l;
    18     return r>a.r;
    19     }
    20 }a[maxn];
    21 priority_queue<Node> q;
    22 bool cmp(Node a,Node b)
    23 {
    24     if(a.l==b.l)
    25     return a.r<b.r;
    26     return a.l<b.l;
    27 }
    28 int main()
    29 {
    30     while(scanf("%d",&n)!=EOF)
    31     {
    32     for(int i=0;i<n;i++)
    33     {
    34         scanf("%d%d",&a[i].l,&a[i].r);
    35         a[i].pos=i;
    36     }
    37     sort(a,a+n,cmp);
    38     q.push(a[0]);
    39     int now=0,ans=1;
    40     use[a[0].pos]=1;
    41     for(int i=1;i<n;i++)
    42     {
    43         if(!q.empty()&&q.top().r<a[i].l)
    44         {
    45         use[a[i].pos]=use[q.top().pos];
    46         q.pop();
    47         }
    48         else
    49         {
    50         ans++;
    51         use[a[i].pos]=ans;
    52         }
    53         q.push(a[i]);
    54     }
    55     printf("%d
    ",ans);
    56     for(int i=0;i<n;i++)
    57         printf("%d
    ",use[i]);
    58     while(!q.empty())
    59         q.pop();
    60     }
    61     return 0;
    62 }    

    AC代码:

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<queue>
     5 #include<cstdio>
     6 using namespace std;
     7 struct TT
     8 {
     9     int x, y, id;
    10 }a[50010];
    11 int vis[50010];
    12 bool cmp( TT m, TT n)
    13 {
    14     if((m.x<n.x) || (m.x==n.x && m.y<n.y)) return true;
    15     return false;
    16 }
    17 bool operator<( TT a, TT b ){
    18     return a.y>b.y;
    19 }
    20 int main()
    21 {
    22     int T,i;
    23     while(~scanf("%d",&T))
    24     {
    25         priority_queue<TT>pp;
    26         memset(vis,0,sizeof(vis));
    27         for(i=1;i<=T;i++)
    28         {
    29             scanf("%d %d",&a[i].x,&a[i].y);
    30             a[i].id = i;
    31         }
    32     TT now;
    33     sort(a+1,a+1+T,cmp);
    34     int ans = 1;
    35     vis[a[1].id]=ans;
    36     pp.push(a[1]);
    37     for(i=2; i<=T; i++)
    38     {
    39          now = pp.top();
    40          if(a[i].x>now.y)
    41          {
    42              vis[a[i].id] = vis[now.id];
    43              pp.pop();//取出该点;
    44              now.y = a[i].y;//更新末尾点;
    45              pp.push(now);
    46          }
    47          else
    48          {
    49              vis[a[i].id] = ++ans;
    50              pp.push(a[i]);
    51          }
    52     }
    53         printf("%d
    ",ans);
    54         for(int i=1;i<=T;i++)
    55         {
    56             printf("%d
    ",vis[i]);
    57         }
    58    }
    59     return 0;
    60 }
  • 相关阅读:
    mysql这个垃圾迁移数据费劲半天
    https请求,可设置请求格式
    mybatis plus 使用
    https请求,get post 通用
    根据经纬度,查询最近距离
    redis与mysql一致性
    导入excel
    在idea中git怎么使用
    spring的传播机制
    通过RequestContextHolder获取HttpServletRequest
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4483851.html
Copyright © 2011-2022 走看看