Cleaning Shifts (poj 2376 贪心)


Language:
Default

Cleaning Shifts

Time Limit: 1000MS   Memory Limit: 65536K
Total 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 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

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;
}
时间: 2024-10-06 20:43:56

Cleaning Shifts (poj 2376 贪心)的相关文章

Cleaning Shifts(POJ 2376 贪心)

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15143   Accepted: 3875 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 co

bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 -- 贪心

3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MB Description 一天有T(1≤T≤10^6)个时段.约翰正打算安排他的N(1≤N≤25000)只奶牛来值班,打扫 打扫牛棚卫生.每只奶牛都有自己的空闲时间段[Si,Ei](1≤Si≤Ei≤T),只能把空闲的奶牛安排出来值班.而且,每个时间段必需有奶牛在值班.  那么,最少需要动用多少奶牛参与值班呢?如果没有办法安排出合理的方案,

Cleaning Shifts / OpenJ_Bailian - 2376

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 bei

poj 2376 贪心 区间覆盖问题

题意: 给n个区间 选择尽量少的区间 覆盖1~T这个区间 如果不能覆盖 输出-1 思路: 经典贪心 区间覆盖 将所有区间按照起点从小到大排序 取终点在最右边的那个区间 code: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<queue>

poj2376 Cleaning Shifts(区间贪心,理解题意)

https://vjudge.net/problem/POJ-2376 题意理解错了!!真是要仔细看题啊!! 看了poj的discuss才发现,如果前一头牛截止到3,那么下一头牛可以从4开始!!! 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7

POJ 2376 Cleaning shifts 贪心 基础题

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13042   Accepted: 3373 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 co

POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

Cleaning Shifts 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

[POJ 2376] Cleaning Shifts

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12874   Accepted: 3331 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 co

POJ2376 Cleaning Shifts 【贪心】

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11542   Accepted: 3004 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 co