poj2376Cleaning Shifts_贪心

来源:互联网 发布:金融行业 数据安全 编辑:程序博客网 时间:2024/06/04 19:37
Cleaning Shifts
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 17935 Accepted: 4569

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.

分析:
这是一个区间调度问题,重要的是找准思路;
之前遇到的类似问题的解决思路有:
(一段时间内参与尽可能多的工作)结束时间越早可选的工作就越多;
(点的覆盖问题)距离非覆盖区域内最左边的点尽可能远,这样覆盖的范围就更大;
(用cow覆盖时间段)在包含最左边开始时间的时间段中,结束时间越晚,覆盖范围越宽,所需cow就越少。重点是抓住每次选取一个cow后,新的开始时间;以及包含开始时间的cow中哪个结束时间最晚。

/** Filename:    code.cpp* Created:     2016-08-04* Author:        yunlong Wang  *[mail:17744454343@163.com]* Desciption:  Desciption*/#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <cmath>#include <vector>#include <bitset>#include <utility>#include <list>#include <sstream>#include <set>#include <functional>using namespace std;#define INT_MAX 1 << 30#define MAX 100typedef long long ll;int n;int t;pair<int, int> ans[25001];//起初定义为pair<int, int> time[25001];显示重定义void solve(){    int answer = 0;    int begin = 1;//未被覆盖的最小的时间段,初始值为1    while (begin <= t)    {        //遍历,找到结束时间最晚的牛        int latest = 0;        for (int i = 0; i < n; i += 1)        {            if (begin >= ans[i].first && begin <= ans[i].second)            {                latest = max(latest,ans[i].second);            }        }        if (latest == 0)//latest未变化,未找到包含begin的,不能完全覆盖,退出        {            printf("-1");            return;        }        //根据结束时间重新定义begin        begin = latest+1;        answer++;    }    printf("%d",answer);}int main(int argc, char const* argv[]){    scanf("%d%d",&n,&t);    for (int i = 0; i < n; i += 1)    {        scanf("%d",&ans[i].first);        scanf("%d",&ans[i].second);    }    solve();    printf("\n");    return 0;}</span>


0 0