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;
    }


  • 相关阅读:
    面向对象编程,其属性特性,用法等
    re正则模块细解,面向对象编程思路的优劣。
    机器人学——1.2-三维空间位姿描述
    机器人学——1.1-二维空间位姿描述
    机器人学——1.0-位置与姿态概述
    latex教程:1.2-latex现状
    latex教程: 1.1-历史
    windows安装opencv
    使用pip安装Opencv
    在Ubuntu上安装opencv-python
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203985.html
Copyright © 2011-2022 走看看