POJ 2376 Cleaning Shifts (greedy)

Source: Internet
Author: User

Description

Farmer John isAssigning some of his N (1<= N <= -, the) Cows to DoSome cleaning chores around the barn. He always wants to having one cow working on cleaning things up and had divided the day into T shifts (1<= T <=1, the, the), the first being shift1And the last being shift T. Each cow isOnly available at some interval of times during the day forWork on cleaning. Any cow that isSelected forCleaning duty would work forThe entirety of her interval. Your Job isTo help Farmer John assign some cows to shifts so (i) every shift have at least one cow assigned to it, and (ii) asFew cows asPossible is involvedinchCleaning. If it isNot possible to assign a cow to each shift, print-1.

Input

1: Both space-2. n+1: Each line contains the start and end times of theinterval during which a cow can work. A Cow starts work at the start time and finishes after the end time. Output1: The minimum number of cows Farmer John needs to hire or-1if are not Possi ble to assign a cow to each shift.

Sample Input

3 Ten 1 7 3 6 6 Ten

Sample Output

2

Hint

This problem have huge input data,use scanf () instead of CIN to read data to avoid time limit exceed.
INPUT DETAILS:
There is 3 cows and 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 is covered. There is no-to-cover all the shifts using fewer than 2 cows.

Source

Usaco 2004 December Silver

Topic:

Given a time T and n time intervals, the minimum number of intervals required to cover the total interval [1,t], unable to cover the area [1,t] when output-1.

For example t=10, there are 3 intervals [1,7],[3,6],[8,10], which requires a minimum of two intervals to cover, select Interval 1 and Interval 3.

Problem Solving Ideas:

Use greedy method. First, the interval is sorted by start time from small to large, and start time is equal by end time from small to large.

1 if the first interval is not starting from 1, it cannot be overwritten, output-1.

2 The maximum value of the end time in the interval at which the current coverage time is 1.

3 loop traversal starts from a range that is not 1 of the start time.

4 Select the interval with the largest end time value in the legal interval, merge to [1,time], and the end time for the selected interval after merging. The so-called legal interval refers to the interval's starting time start<=time+1, in order to merge with the interval [1,time]. If no legal interval is found or the end of the interval is found to be smaller than time, you cannot overwrite and end the loop.

5 After the end of the loop, the result is output based on whether time is greater than or equal to T.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <stdlib.h>6 using namespacestd;7 #defineN 260008 structNode9 {Ten     intst,ed; One }p[n]; A BOOLCMP (Node A,node b) { -     if(a.st!=b.st) -       returna.st<B.st; the     returna.ed<B.Ed; - } - intMain () - { +     intn,t; -      while(SCANF ("%d%d", &n,&t) = =2) +     { A          for(intI=0; i<n;i++) at         { -scanf"%d%d",&p[i].st,&P[i].ed); -         } -          -Sort (p,p+n,cmp); -          in         intI=0; -         intTime=P[i].ed; to         if(p[i].st!=1) +         { -printf"-1\n"); the             Continue; *         } $i++;Panax Notoginseng          while(I<n && p[i].st==1) -         { theTime=P[i].ed; +i++; A         } the         intans=1; +          -          while(time<t) $         { $              -             if(i>=N) -                Break; the             inttmp=i; -             intw=P[i].ed;Wuyii++; the              while(I<n && p[i].st<=time+1) -             { Wu                 if(w<P[i].ed) -                 { Abouttmp=i; $w=P[i].ed; -                 } -i++; -             } A              +             if(W<=time | | p[tmp].st>time+1) the             {  -                   Break;  $             } the             Else the             { theTime=W; theans++; -             } in              the         } the         if(time<t) Aboutans=-1; the          theprintf"%d\n", ans); the     } +     return 0; -}
View Code

POJ 2376 Cleaning Shifts (greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.