Cleaning Shifts (poj 2376 贪心)

来源:互联网 发布:淘宝卖家如何拉客户端 编辑:程序博客网 时间:2024/04/26 08:16
Language:
Cleaning Shifts
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12111 Accepted: 3155

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.

Source

USACO 2004 December Silver

题意:给出区间[1,N]和N个小区间,用小区间去覆盖[1,N],问能否覆盖完全,若不能则输出-1,否则输出所需要的小区间的最少数目。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 25005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b)  for(i = a; i <= b; i++)#define FRL(i,a,b)  for(i = a; i < b; i++)#define mem(t, v)   memset ((t) , v, sizeof(t))#define sf(n)       scanf("%d", &n)#define sff(a,b)    scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf          printf#define DBG         pf("Hi\n")typedef long long ll;using namespace std;struct Cow{    int s,t;}cow[maxn];int n,T;int cmp(Cow a,Cow b){    return a.s<b.s;}int main(){    int i,j;    while (~sff(n,T))    {        FRL(i,0,n)        sff(cow[i].s,cow[i].t);        sort(cow,cow+n,cmp);    //按照开始时间从小到大排序        if (cow[0].s>1){   //若最小的开始时间比1大直接输出-1            pf("-1\n");            continue;        }        int t=1,ans=0,maxx=-1,flag=1,i=0;        while (i<n)        {            while (cow[i].s<=t&&i<n)  //求出开始时间在t及t时刻之前的cow中的最大结束时间            {                maxx=max(maxx,cow[i].t);                i++;            }            if (maxx==-1){  //表明没找到,中间断了                flag=0;                break;            }            t=maxx+1;  //将最大的结束时间赋给t            ans++;            if (maxx>=T) break;   //最大时间已经达到T了break            maxx=-1;        }        if (maxx<T) flag=0;        if (!flag)            pf("-1\n");        else            pf("%d\n",ans);    }    return 0;}


2 0