POJ 2376 Cleaning Shifts

Source: Internet
Author: User

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

Cleaning Shifts
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12604 Accepted: 3263

Description

Farmer John is assigning some of he n (1 <= n <= 25,000) cows to do some 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,000 , the first being shift 1 and the last being shift T.

Each cow is a available at some interval of times during the day for work on cleaning. Any cow that's selected for cleaning duty would work for the entirety of her interval.

Your job is-to-help Farmer John Assign some cows-shifts so (i) every shift have at least one cow assigned to it, an D (ii) as few cows as possible is 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 isn't possible to assign a cow to each shift.

Sample Input

3 101 73) 66 10

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. Analysis: Test instructions is to give you a small interval of n (n, m) and a t,t for a large interval [1, T], which requires you to find out the minimum use of a small interval to completely cover a large interval. At first, WA, because the concept of "full coverage" was misunderstood. That is the following data:
1 3 10//3 represents the number of inter-cell, 10 represents a large interval length.  21536849                    

The above data my program output is:-1

Because there are two intervals without "full coverage", [5, 6] and [8, 9].

But the correct answer is 3.

WA Code:

1#include <cstdio>2#include <algorithm>3 using namespacestd;4 structP5 {6     intx, y;7     BOOL operator< (ConstP & P)Const8     {9         returnX < p.x | | (x = = p.x && y >p.y);Ten     } One}a[25010]; A  - intMain () - { the     intn,t; -      while(~SCANF ("%d%d",&n,&t)) -     { -          for(inti =0; I < n;i++)  +scanf"%d%d",&a[i].x,&a[i].y); -Sort (A, A +n); +         intres =1, S; A         if(a[0].x >1) atprintf"-1\n"); -         Else -         { -s = a[0].y; -              for(inti =1; i < n && s <t;) -             { in                 intTMP =0; -                  while(I < n && a[i].x <=s) to                 { +TMP =MAX (TMP, A[I].Y); -i++; the                 } *                 if(tmp >s) $                 {Panax Notoginsengs =tmp; -res++; the                 } +                 Else A                      Break; the             } +         } -         if(S >=t) $printf"%d\n", res); $         Else -printf"-1\n"); -     } the     return 0; -}
View Code

AC Code:

1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5 structp{6     intx, y;7     BOOL operator< (ConstP & P)Const {8         returnX < p.x | | (x = = p.x && y >p.y);9     }Ten}a[25010]; One  A intMain () { -     intn,t; -      while(~SCANF ("%d%d",&n,&t)) { the          for(inti =0; I < n;i++)  -scanf"%d%d",&a[i].x,&a[i].y); -              -Sort (A, A +n); +          -         intres =1, S; +         if(a[0].x >1) { Aprintf"-1\n"); at             Continue; -}Else { -s = a[0].y; -              for(inti =1; i < n && s <t;) { -                 intTMP =0; -                  while(I < n && a[i].x <= s +1) { inTMP =MAX (TMP, A[I].Y); -i++; to                 } +                 if(tmp >s) { -s =tmp; theres++; *}Else  Break; $             }Panax Notoginseng         } -              the         if(S >=t) { +printf"%d\n", res); A}Else { theprintf"-1\n"); +         } -     } $     return 0; $}

POJ 2376 Cleaning Shifts

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.