Cleaning Shifts (POJ 2376 greedy)

Source: Internet
Author: User

Language:DefaultCleaning Shifts
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12111 Accepted: 3155

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.

Source

Usaco 2004 December Silver

Test instructions: Give the interval [1,n] and N small intervals, with a small interval to cover [1,n], ask whether the coverage is complete, if not output-1, otherwise the minimum number of cells required for output.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 25005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FRL (i,a,b) for (i = A; I < b; i++) #define Mem (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, & AMP;B) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n" ) typedef long Long ll;using namespace std;struct cow{int s,t;} Cow[maxn];int n,t;int cmp (Cow A,cow b) {return A.S&LT;B.S;}    int main () {int i,j;  while (~SFF (n,t))  {FRL (i,0,n) SFF (cow[i].s,cow[i].t);    Sort (cow,cow+n,cmp);            According to the start time from small to large sort if (cow[0].s>1) {//If the minimum start time is greater than 1 direct output-1 pf (" -1\n");        Continue        } int t=1,ans=0,maxx=-1,flag=1,i=0;                while (I<n) {while (cow[i].s<=t&&i<n)//To find the maximum end time of the start time in cow before T and T time {                Maxx=max (MAXX,COW[I].T);            i++;                } if (maxx==-1) {//Indicates not found, middle broken flag=0;            Break  } t=maxx+1;            Assigns the maximum end time to T-ans++;   if (maxx>=t) break;        The maximum time has reached T-break maxx=-1;        } if (maxx<t) flag=0;        if (!flag) pf (" -1\n");    Else PF ("%d\n", ans); } return 0;}


Cleaning Shifts (POJ 2376 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.