Cleaning Shifts

Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:31249   Accepted: 7684

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.

 

 

题目大意就是说,从1到T区间内,必须保证每个点都有牛在工作,给出每头牛的工作时间,求需用到的最小的牛的数量。

区间个数最小.

贪心.

给几组数据

 

 4 20
1 5
3 8
6 10
11 20
 
 

5 20
1 6
2 7
3 8
9 16
17 20

4

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #define N 25005
 4 using namespace std;
 5 struct Node{
 6     int l,r;
 7 }node[N];
 8 
 9 bool cmp(Node a,Node b){
10     if(a.l==b.l)
11         return a.r>b.r;
12     return a.l<b.l;
13 }
14 int vis[1000005];
15 int n,m;
16 int main(){
17     scanf("%d %d",&n,&m);
18     bool x = false,y = false;
19     for(int i=0;i<n;i++){
20         scanf("%d%d",&node[i].l,&node[i].r);
21         if(node[i].l==1) x = true;
22         if(node[i].r==m) y = true;
23     }
24     if(!x||!y){
25         cout<<"-1"<<endl;
26         return 0;
27     }
28     sort(node,node+n,cmp);
29     int end = 0;
30     int begin = 0;
31     int ans = 0;
32     vis[0] = 1;
33     for(int i=0;i<n;i++){
34         if(node[i].l>end+1){
35             cout<<"-1"<<endl;
36             return 0;
37         }
38         if(node[i].r>end){
39             if(vis[node[i].l]==0||vis[node[i].l-1]==ans){
40                 ans++;
41             }
42             for(int j=end+1;j<=node[i].r;j++){
43                 vis[j] = ans;
44             }
45             end = node[i].r;
46         }
47         if(end>=m)
48             break;
49     }
50     cout<<vis[m]<<endl;
51     return 0;
52 }

 

posted @ 2018-08-20 21:22  #忘乎所以#  阅读(438)  评论(0编辑  收藏  举报