POJ-2376

Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23483   Accepted: 5872

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 10
1 7
3 6
6 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.

 

题意:

给出n个区间,求覆盖1~n的最小区间数。

 

将区间按左区间最小排序,若左区间相等,则将右区间最大优先。

选取区间最大的。

 

 1 //#include<bits/stdc++.h>
 2 #include<cstdio>  
 3 #include<cstring>  
 4 #include<algorithm>
 5 #include<iostream>
 6 using namespace std;
 7 
 8 struct node{
 9     int x;
10     int y;
11 }a[30000];
12 
13 int cmp(node a,node b){
14     if(a.x!=b.x) 
15     return a.x<b.x;
16     else
17     return a.y>b.y;
18 }
19 
20 int main(){
21     ios::sync_with_stdio(false);
22     int n,t;
23     while(cin>>n>>t&&n&&t){
24         for(int i=0;i<n;i++){
25             cin>>a[i].x>>a[i].y;
26         }
27         sort(a,a+n,cmp);
28         if(a[0].x!=1){
29             cout<<"-1"<<endl;
30             continue;
31         }
32         int temp=0,k=1,ri=a[0].y;
33         for(int i=1;i<n;i++){
34             if(a[i].x>ri+1){//前一个即为可选区间 
35                 k++;
36                 ri=temp;
37             }
38             if(a[i].x<=ri+1){
39                 if(a[i].y>temp){
40                     temp=a[i].y;//更新最右区间 
41                 }
42                 if(a[i].y==t){
43                     k++;
44                     ri=t;
45                     break;
46                 }
47             }
48         }
49         if(ri==t){
50             cout<<k<<endl;
51         }
52         else{
53             cout<<"-1"<<endl;
54         }
55     }
56     return 0;
57 }

 

posted @ 2017-08-07 11:22  Kiven#5197  阅读(278)  评论(0编辑  收藏  举报