POJ_2376_Cleaning Shifts (Greedy)

Source: Internet
Author: User
Tags cmath

POJ_2376_Cleaning Shifts (Greedy)
Cleaning Shifts

Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:12788   Accepted:3312

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 101 73 66 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.


The farmer has a nheaded ox. Now he wants some cows to do housework. Then he divides a day into T time points, that is, the time point of a day is the interval [1, T]. He wants cows to do housework at any point in time. Now, I will give you a working time for each ox and ask if you can meet his/her requirements with the least number of cows, that is, cover the day with the least amount of time ([1, T]). If not,-1 is output; otherwise, the minimum number of cattle is output.

Analysis: greedy question. The question clearly means that the interval with the minimum value overwrites the large interval. Sort the order of these time periods (see the Code). This sorting should be fine. Then, the first ox must be selected, starting from this ox and selecting the next ox. How can I choose the next ox? That is, in the cattle that meet the conditions (note: the cattle that meet the conditions only need to start working time> = cow [0]. y + 1), select the one with the largest right boundary value, because this will overwrite the most time range. And so on.


Code List:
#include
# Include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Include using namespace std; typedef long ll; const int maxn = 25000 + 5; const int maxd = 1000000 + 5; struct Edge {int x; int y ;} cow [maxn]; int N, T; int X, Y; bool cmp (Edge a, Edge B) {if (. x = B. x) return. y> B. y; return. x Maxy) {maxy = cow [End]. y ;}} if (maxy! = Start) {ans ++; Start = maxy;} else {if (End = N-1) {// The interval break has been overwritten ;} else {// indicates that some intermediate time points do not cover printf ("-1 \ n"); return ;}} printf ("% d \ n", ans ); return;} int main () {input (); solve (); return 0 ;}


Related Article

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.