Poj 2376 Cleaning Shifts, poj2376

Source: Internet
Author: User

Poj 2376 Cleaning Shifts, poj2376
Cleaning Shifts

Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:18151   Accepted:4620

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.

Source

USACO 2004 December Silver greedy algorithm: Given T time ranges, the range is [1, T]. Different cows have different working hours, and the minimum number of jobs can cover this range. First, it is sorted by the time when the ox starts to work, and then the starting point is continuously updated = end point + 1, which can cover the ox at the start time, each time you select the newest ox, update the specific AC code at the end:
# Define _ CRT_SECURE_NO_DEPRECATE # include <iostream> # include <algorithm> using namespace std; const int N_max = 25000; pair <int, int> cows [N_max]; int N, T; bool cmp (const pair <int, int> & a, const pair <int, int> & B) {return (. first <B. first | (. first = B. first &. second> B. second);} int solve () {int used_cows = 0; int end = 0, num = 0; while (end <T) {int begin = end + 1; // at this time, the end is the end time of the previous ox's work. At this time, the begin indicates that the current ox's work start time must be before begin for (int I = num; I <N; I ++) {// select a new ox, so that the later the end time, the better if (cows [I]. first <= begin) {if (cows [I]. second> = begin) // do not forget to add equal to. It is possible that there is only one worker interval, for example, 3-3 end = max (end, cows [I]. second); // select one of the available cattle so that the working time is up to the latest} else {num = I; // You can select any cattle that do not meet the requirements, new ox break;} // determines whether the selected ox meets the requirements, that is, its end time must be later than begin, otherwise, an interval cannot be overwritten if (begin> end) {// at this time, begin is greater than or equal to the start time of the selected ox, the end is the end time of the current job. return-1;} else used_cows ++;} return used_cows;} int main () {scanf ("% d", & N, & T); for (int I = 0; I <N; I ++) scanf ("% d", & cows [I]. first, & cows [I]. second); sort (cows, cows + N, cmp); cout <solve () <endl; 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.