zoukankan      html  css  js  c++  java
  • poj2376(Cleaning Shifts)

    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.

    区间覆盖问题,贪心,对左端点从小到大排序,相同时右端点从大到小排序。记录已处理区间所能达到的最右端值,枚举左端点小于该值的区间中右端点最大的即可,注意判断无解情况。

    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <set>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <algorithm>
    #include <functional>
    #include <iomanip>
    #include <limits>
    #include <new>
    #include <utility>
    #include <iterator>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const double PI = acos(-1.0);
    int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
    const int maxn = 25010;
    
    struct cow
    {
        int start, over;
        bool operator < (const cow& b) const
        {
            if (start != b.start)
                return start < b.start;
            return over > b.over;
        }
    };
    
    cow s[maxn];
    
    int main()
    {
        int n, t;
        scanf("%d%d", &n, &t);
        for (int i = 0; i < n; ++i)
            scanf("%d%d", &s[i].start, &s[i].over);
        sort(s, s+n);
        int ans = 0, last = 1, tmp = 0;
        while (last <= t)
        {
            if (tmp == n || s[tmp].start > last)
                break;
            int tmplast = 0;
            for (int i = tmp; i < n; ++i)
            {
                if (s[i].start > last)
                {
                    tmp = i;
                    break;
                }
                if (tmplast < s[i].over)
                    tmplast = s[i].over;
                tmp = i + 1;
            }
            ans++;
            last = tmplast + 1;
        }
        if (last <= t)
            ans = -1;
        cout << ans << endl;
        return 0;
    }


  • 相关阅读:
    使用Docker容器来源码编译etcd
    PHP开发第一个扩展
    CI框架SESSION重写
    XMLHttpRequest的跨域请求
    PHP哈希表碰撞攻击
    empty、isset、is
    PHP实现4种排序算法
    C实现9种排序算法
    Debian、Ubuntu常用命令大全
    Java中 int和Integer的区别+包装类
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203985.html
Copyright © 2011-2022 走看看