`
Simone_chou
  • 浏览: 192514 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Cleaning Shifts(贪心)

    博客分类:
  • POJ
 
阅读更多
Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10970   Accepted: 2877

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 ~ 25000),t(1 ~ 1000000),代表有 n 个区间,总长为 t,寻找最小的区间使之覆盖整个区间 1 ~ t。输出最小的区间数。

 

      思路:

      贪心。将起点从小到大排序一遍,每次选择到达最远的终点的区间,直到 t 为止,如果不到达则立即跳出循环输出 -1。

 

      AC:

 

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef struct { int s, e; }node;

node no[25005];

bool cmp (node a, node b) { return a.s < b.s; }

int main() {

    int n, t;
    scanf("%d%d", &n, &t);

    for (int i = 0; i < n; ++i)
        scanf("%d%d", &no[i].s, &no[i].e);

    sort(no, no + n, cmp);

    int ans = 0, i = 0, ee = 0;
    while (i < n) {
        int max_ee = 0;
        while (i < n && (no[i].s <= ee || no[i].s == ee + 1)) {
            if (no[i].e > max_ee) {
                max_ee = no[i].e;
            }
            ++i;
        }

        if (max_ee) {
            ee = max_ee;
            ++ans;
        } else break;

        if (ee == t) break;
    }

    if (ee == t) printf("%d\n", ans);
    else printf("-1\n");

    return 0;
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics