zoukankan      html  css  js  c++  java
  • Stall Reservations

    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 struct node{
     2     int a;
     3     int b;
     4     int flag;
     5      friend bool operator <(node node1,node node2)  
     6     {  
     7         //<为从大到小排列,>为从小到大排列  
     8         return node1.b<node2.b;  
     9     }  
    10 }n[5];
     1 #include<iostream>
     2 #include<algorithm> 
     3 #include<queue>
     4 using namespace std;
     5 
     6 struct node{
     7     int a; //开始时间 
     8     int b; //结束时间 
     9     int c; //序列号 
    10     int flag;    //区间安排序号 
    11     friend bool operator <(node node1,node node2) //重载比较操作符函数 
    12     {
    13         return node1.b > node2.b;
    14     }
    15 }cows[50005];
    16 
    17 bool cmp1(node t1,node t2)  //根据区间排序 
    18 {
    19     if(t1.a != t2.a) return t1.a < t2.a;
    20     else return t1.b < t2.b;
    21 }
    22 
    23 bool cmp2(node t1,node t2) //根据序列号排序 
    24 {
    25      return t1.c < t2.c;
    26 }
    27 
    28 
    29 int main()
    30 {
    31     int n;
    32     priority_queue<node>que;
    33     while(~scanf("%d",&n))
    34     {
    35         for(int i = 0; i < n; i++)
    36         {
    37             scanf("%d %d",&cows[i].a,&cows[i].b);
    38             cows[i].c = i;
    39         }
    40         sort(cows,cows+n,cmp1);  
    41         int  ans = 1;
    42         cows[0].flag = ans;
    43         que.push(cows[0]);
    44         
    45         for(int i = 1 ; i < n; i++)
    46         {
    47             if(que.top().b >= cows[i].a)  //此时cows的区间与que内的任意区间有重合 
    48             {
    49                 ans++;
    50                 cows[i].flag = ans;
    51             }
    52             else
    53             {
    54                 cows[i].flag = que.top().flag;
    55                 que.pop();
    56             }
    57             que.push(cows[i]);
    58         }
    59 
    60         sort(cows,cows+n,cmp2);  
    61         cout<<ans<<endl;
    62         for(int i = 0; i < n; i++)
    63         {
    64             printf("%d
    ",cows[i].flag);
    65         }
    66     }
    67     return 0;
    68 }
  • 相关阅读:
    确定方法返回位置为泛型的类型
    centos7 svn服务器搭建
    centos7优化启动项,关闭一些不必要开启的服务
    使用windos电脑模拟搭建集群(四)web环境 linux+nginx+jdk+tomcat
    java使用BeanUtils封装file类型表单数据到一个对象中
    表单隐藏域
    本地yum仓库的搭建
    centos7 挂载exfat格式的u盘
    三体
    海盗分金的故事
  • 原文地址:https://www.cnblogs.com/jj81/p/8387721.html
Copyright © 2011-2022 走看看