• Cleaning Shifts


    Cleaning Shifts
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions:31249   Accepted: 7684

    Description

    Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T. 

    Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval. 

    Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

    Input

    * Line 1: Two space-separated integers: N and T 

    * Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.

    Output

    * Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

    Sample Input

    3 10
    1 7
    3 6
    6 10

    Sample Output

    2

    Hint

    This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 

    INPUT DETAILS: 

    There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10. 

    OUTPUT DETAILS: 

    By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

    题目大意就是说,从1到T区间内,必须保证每个点都有牛在工作,给出每头牛的工作时间,求需用到的最小的牛的数量。

    区间个数最小.

    贪心.

    给几组数据

     4 20
    1 5
    3 8
    6 10
    11 20
     
     

    5 20
    1 6
    2 7
    3 8
    9 16
    17 20

    4

     1 #include <iostream>
     2 #include <algorithm>
     3 #define N 25005
     4 using namespace std;
     5 struct Node{
     6     int l,r;
     7 }node[N];
     8 
     9 bool cmp(Node a,Node b){
    10     if(a.l==b.l)
    11         return a.r>b.r;
    12     return a.l<b.l;
    13 }
    14 int vis[1000005];
    15 int n,m;
    16 int main(){
    17     scanf("%d %d",&n,&m);
    18     bool x = false,y = false;
    19     for(int i=0;i<n;i++){
    20         scanf("%d%d",&node[i].l,&node[i].r);
    21         if(node[i].l==1) x = true;
    22         if(node[i].r==m) y = true;
    23     }
    24     if(!x||!y){
    25         cout<<"-1"<<endl;
    26         return 0;
    27     }
    28     sort(node,node+n,cmp);
    29     int end = 0;
    30     int begin = 0;
    31     int ans = 0;
    32     vis[0] = 1;
    33     for(int i=0;i<n;i++){
    34         if(node[i].l>end+1){
    35             cout<<"-1"<<endl;
    36             return 0;
    37         }
    38         if(node[i].r>end){
    39             if(vis[node[i].l]==0||vis[node[i].l-1]==ans){
    40                 ans++;
    41             }
    42             for(int j=end+1;j<=node[i].r;j++){
    43                 vis[j] = ans;
    44             }
    45             end = node[i].r;
    46         }
    47         if(end>=m)
    48             break;
    49     }
    50     cout<<vis[m]<<endl;
    51     return 0;
    52 }
  • 相关阅读:
    观察者模式股票提醒
    中介者模式虚拟聊天室
    模板方法模式数据库的连接
    职责链模式财务审批
    期末总结
    软件需求分析考试
    tomcat启动极其慢的解决方法困扰我一年多的问题终于解决
    状态模式银行账户
    解释器模式
    动态加载JS文件提升访问网站速度
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/9508197.html
Copyright © 2020-2023  润新知