zoukankan      html  css  js  c++  java
  • POJ 2376 Cleaning Shifts

    Cleaning Shifts

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 9   Accepted Submission(s) : 2
    Problem 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 <br> <br>* 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
     
    每头牛有一定的工作时间段,问最少用多少头牛才能保证在所有时间内都有牛在工作
    用贪心算法,每次选取工作结束时间最晚的牛。
    记住:
    2 10
    1 5
    1 6
    也是可以的,不一定要1 5和5 10
     
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <cstring>
    #include <queue>
    using namespace std;
    struct node
    {
        int s, e;
    }a[1000005];
    bool cmp(node a, node b)//贪心
    {
        if (a.s == b.s)//开始时间短的在前
            return a.e<b.e;
        else
            return a.s<b.s;
    }
    int main()
    {
        int n, t;
        int i;
        cin >> n >> t;
        for (i = 1; i <= n; i++)
        {
            cin >> a[i].s >> a[i].e;
        }
        a[n + 1].s = 999999999;//最后一个开始时间设为无穷大
        sort(a + 1, a + 1 + n, cmp);
        
        int mm = 0;
        int ans = 0;
        bool f = 0;
        int ss =0;
        for (i = 1; i <= n; i++)
        {
            if (a[i].s <=ss+1 )//是ss+1
            {
                if (a[i].e > mm)
                {
                    mm = a[i].e;
                    f = 1;
                }
                if (a[i + 1].s > ss+1 && f == 1)
                {
                    ss = mm;
                    ans++;
                    f = 0;
                }
            }
        }
        if (ss>=t) cout << ans << endl;
        else cout << "-1" << endl;
        return 0;
    }
  • 相关阅读:
    系统数据库如何恢复Master数据库
    在Orderby子句中使用CASE 语句
    SQL NOTECURSOR
    MVC 中静态文件部分动态化
    SQL NOTCTE
    Get data style from Excel
    SQL NOTPARTITION
    LAMDA表达式学习
    SQL NOTEVARIABLE
    起II6.0中使用windows2003自带的虚拟主机管理系统
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8404859.html
Copyright © 2011-2022 走看看