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;
    }
  • 相关阅读:
    06-按钮Button
    05-文本视图TextView
    02-运行配置AndroidManifest.xml
    01-编译配置文件build.gradle详解
    jquery获取焦点和失去焦点
    Liunx下安装Oracle11g时Oracle Grid安装包下载向导
    配置虚拟机上的RedHat6 Linux系统的网络(选择的是仅主机模式)
    Redhat镜像-RHEL-官方镜像下载大全
    在VMware中创建一个新的虚拟机 ,安装Linux4.X系统 ,之后在此基础上安装openfiler(网络存储管理实用程序)
    Oracle18C安装后首次创建数据库并用sql developer 创建连接和用户
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8405057.html
Copyright © 2011-2022 走看看