zoukankan      html  css  js  c++  java
  • POJ 2376 Cleaning Shifts

    Cleaning Shifts
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8457   Accepted: 2263

    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 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 
     5 using namespace std;
     6 
     7 typedef struct
     8 {
     9     int Start;
    10     int End;
    11 } COW;
    12 
    13 int n,t;
    14 COW cow[25010];
    15 
    16 bool cmp(COW a,COW b)
    17 {
    18     return a.Start<b.Start||(a.Start==b.Start&&a.End<b.End);
    19 }
    20 
    21 int main()
    22 {
    23     while(scanf("%d %d",&n,&t)==2)
    24     {
    25         for(int i=0;i<n;i++)
    26             scanf("%d %d",&cow[i].Start,&cow[i].End);
    27 
    28         sort(cow,cow+n,cmp);
    29 
    30         bool flag=true;
    31         int End=0,pos=0,ans=0;
    32         while(End<t)
    33         {
    34             int maxx=-1;
    35             for(;cow[pos].Start<=End+1;pos++)
    36             {
    37                 if(cow[pos].End>maxx)
    38                     maxx=cow[pos].End;
    39             }
    40             if(maxx==-1)
    41             {
    42                 flag=false;
    43                 break;
    44             }
    45             End=maxx;
    46             ans++;
    47         }
    48         if(flag)
    49             cout<<ans<<endl;
    50         else
    51             cout<<"-1"<<endl;
    52     }
    53 
    54     return 0;
    55 }
    [C++]
  • 相关阅读:
    Unsafe(转载) 规格严格
    MySQL 中文 规格严格
    2007“奥普迪杯”开放式实时在线辞典系统设计大赛
    2007“奥普迪杯”开放式实时在线辞典系统设计大赛
    2007年个人回忆与总结
    蔡学镛:2008编程语言走势解盘
    用scanf实现gets的功能
    2007年个人回忆与总结
    用scanf实现gets的功能
    初学入门:如何有效编写软件的75条建议
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3246496.html
Copyright © 2011-2022 走看看