POJ 2376 Cleaning Shifts

Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18180   Accepted: 4630

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.

Source

 

    区间覆盖问题。数轴上有n个闭区间 [ai, bi],选择尽量少的区间覆盖一条指定线段[s,t]。题目意义的区间与一般的区间不太相同,因为按照题意,[1,t]与[t+1,n]是可以覆盖[1,n]的。

    见《算法竞赛入门经典(第二版)》第8章。

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <map>
 4 #include <vector>
 5 #include <functional>
 6 #include <string>
 7 #include <cstring>
 8 #include <queue>
 9 #include <set>
10 #include <cmath>
11 #include <cstdio>
12 using namespace std;
13 #define IOS ios_base::sync_with_stdio(false)
14 #define TIE std::cin.tie(0)
15 #define MIN2(a,b) (a<b?a:b)
16 #define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c))
17 #define MAX2(a,b) (a>b?a:b)
18 #define MAX3(a,b,c)  (a>b?(a>c?a:c):(b>c?b:c))
19 typedef long long LL;
20 typedef unsigned long long ULL;
21 const int INF = 0x3f3f3f3f;
22 const double PI = 4.0*atan(1.0);
23 
24 int n, t, ans;
25 typedef struct Interval{
26     int st, et;
27     bool operator<(const Interval &a) const{
28         return st < a.st;
29     }
30 }I;
31 I a[25005];
32 
33 bool solve()
34 {
35     int i = 0, et = 0, tmp;
36     ans = 0;
37     while (i < n&&et < t){
38         if (a[i].st>et + 1) return false;
39         tmp = -1;
40         while (i < n&&a[i].st <= et + 1){
41             tmp = MAX2(tmp, a[i].et); i++;
42         }
43         et = tmp;
44         ans++;
45     }
46     if (et < t) return false;
47     else return true;
48 }
49 
50 int main()
51 {
52     scanf("%d%d", &n, &t);
53         for (int i = 0; i < n; i++)
54             scanf("%d%d", &a[i].st, &a[i].et);
55     sort(a, a + n);
56     printf("%d\n", solve() ? ans : -1);
57 }

 

posted @ 2016-09-09 15:44  Cumulonimbus  阅读(186)  评论(0编辑  收藏  举报