zoukankan      html  css  js  c++  java
  • poj 3190 Stall Reservations

    原题链接:http://poj.org/problem?id=3190

    题目大意: 一些很挑剔的奶牛需要在特定的时间内挤奶,一个挤奶棚每次只能有一头奶牛挤奶,求出最少需要的挤奶棚的数量;

    思路:贪心+优先队列; 先用开始挤奶的时间顺序排序,然后再按照结束时间早的顺序存到优先队列中; 这样每次判断一下当前是否有空的挤奶棚可以用就行了,如果有直接用,没有的话奶棚数量加一;

    代码:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <stack>
    #include <queue>
    #include <cmath>
    #define ll long long
    #define pi 3.1415927
    using namespace std;
    struct node {
    int a,b,c;
    }a[50005];
    bool cmp (node x, node y)
    {
        if (x.a!=y.a)
            return x.a<y.a;
        return x.b<y.b;
    }
    struct cow{
    int i,e;
    friend bool operator < (cow x, cow y)
        {
        return x.e>y.e;
        }
    }b;
    priority_queue <cow> q ;
    int c[50005];
    int main ()
    {
        int n,m,i,t,j,k,sum=0;
        scanf("%d",&n);
        for (i=0;i<n;++i){
            scanf("%d %d",&a[i].a,&a[i].b);
            a[i].c=i;
            ///a,b 存挤奶的开始和结束时间   c存第几头奶牛
        }
        sort(a,a+n,cmp);
        int p=1;
        sum=0;
        for (i=0;i<n;++i)
        {
            if(q.empty())
            {
                c[a[i].c]=p++;
                sum++;
                b.i=a[i].c; b.e=a[i].b;
                q.push( b );
                continue;
            }
    
            if (q.top().e < a[i].a )  
            ///如果当前奶牛的开始时间大于队列顶部元素的结束时间代表有空的挤奶棚可以用
            {
                c[a[i].c] = c[ q.top().i ];
                q.pop();
                b.i=a[i].c;  b.e=a[i].b;
                q.push( b );
            }
            else
            {
                c[a[i].c]=p++;
                sum++;
                b.i=a[i].c;  b.e=a[i].b;
                q.push( b );
            }
        }
        printf("%d
    ",sum);
        for (i=0;i<n;++i)
            printf("%d
    ",c[i]);
    
        return 0;
    }
  • 相关阅读:
    WPF多线程问题
    SQL 使用经验
    [转]express 路由控制--next
    [转]浅谈Web缓存
    [转]一份优秀的前端开发工程师简历是怎么样的?
    http
    [转]HTTP详解(1)-工作原理
    [转]使用Flexible实现手淘H5页面的终端适配
    [转]理解$watch ,$apply 和 $digest --- 理解数据绑定过程
    GMT时间
  • 原文地址:https://www.cnblogs.com/blowhail/p/11168218.html
Copyright © 2011-2022 走看看