zoukankan      html  css  js  c++  java
  • poj3190 Stall Reservations (贪心+优先队列)

    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
     

     

    Explanation of the sample:

    Here's a graphical schedule for this output:

    Time 1 2 3 4 5 6 7 8 9 10

    Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..

    Stall 3 .. .. c3>>>>>>>>> .. .. .. ..

    Stall 4 .. .. .. c5>>>>>>>>> .. .. ..Other outputs using the same number of stalls are possible.

     

    思路:

    首先根据挤奶时间的先后顺序排序。。。然后将第一头牛加入优先队列。。然后就是加入优先队列的牛应该根据越早结束挤奶那么优先级更高,如果时间结束点相等,那么开始时间早的优先级高。。。

    然后从前向后枚举。如果碰到有牛的挤奶时间的开始值大于优先队列的首部的结束值,那么说明这两头牛可以一起公用一个挤奶房。。然后从优先队列中删除这头牛。。那么这个问题就得到解决了。。。

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int maxn = 50000 + 10;
    int order[maxn];
    
    struct Node
    {
        int st, en, pos;
        friend bool operator<(Node a, Node b)
        {
            if (a.en == b.en)
                return a.st<b.st;
            return a.en>b.en;
        }
    }node[maxn];
    
    bool cmp(Node a, Node b)
    {
        if (a.st == b.st)
            return a.en<b.en;
        else
            return a.st<b.st;
    }
    
    priority_queue<Node>Q;
    
    int main()
    {
        int n, ans;
        while (~scanf("%d", &n))
        {
            for (int i = 1; i <= n; i++)
            {
                scanf("%d%d", &node[i].st, &node[i].en);
                node[i].pos = i;
            }
            sort(node + 1, node + 1 + n, cmp);
            ans = 1;
            Q.push(node[1]);
            order[node[1].pos] = 1;
            for (int i = 2; i <= n; i++)
            {
                if (!Q.empty() && Q.top().en<node[i].st)
                {
                    order[node[i].pos] = order[Q.top().pos];
                    Q.pop();
                }
                else
                {
                    ans++;
                    order[node[i].pos] = ans;
                }
                Q.push(node[i]);
            }
            printf("%d
    ", ans);
            for (int i = 1; i <= n; i++)
                printf("%d
    ", order[i]);
            while (!Q.empty())  Q.pop();
        }
        return 0;
    }
  • 相关阅读:
    将Microsoft SQL Server 2000数据库转换成MySQL数据库
    centos7 升级php版本
    Jquery Ajax方法传递json到action
    2015/12/7
    sql server 2008 评估期已过期
    C# 邮件发送注意事项
    ReSharper warning: Virtual member call in a constructor
    EF code first 生成edmx文件
    EF 已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭
    C# 发送邮件
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271297.html
Copyright © 2011-2022 走看看