zoukankan      html  css  js  c++  java
  • POJ 3190(贪心+优先队列)

    题目:https://vjudge.net/problem/POJ-3190

    Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows.

    Help FJ by determining:
    • The minimum number of stalls required in the barn so that each cow can have her private milking period
    • An assignment of cows to these stalls over time
    Many answers are correct for each test dataset; a program will grade your answer.

    Input

    Line 1: A single integer, N

    Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

    Output

    Line 1: The minimum number of stalls the barn must have.

    Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

    Sample Input

    5
    1 10
    2 4
    3 6
    5 8
    4 7

    Sample Output

    4
    1
    2
    3
    2
    4

    Hint

    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.
     
     
    题意:n头牛
          分别有挤奶的开始时间和结束时间
    求最少需要的挤奶机的台数
    分别输出每头奶牛属于哪台机器挤的奶
     
     
     
    思路:   贪心思想: 一开始按结束时间想是错的    先按开始时间从小到大开始排序   ,然后遍历用优先队列按结束时间从小到大的顺序去存,
    ,,然后每次只需要检查当前是否有奶牛的挤奶工作已经完成的机器即可,若有,则换那台机器进行工作。若没有,则加一台新的机器。
    存下这些挤奶机器的编号
     
     
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<set>
    #include<algorithm>
    #include<map>
    #define maxn 1000000+10
    using namespace std;
    int used[maxn];   //存机器的编号
    struct node{
       int st,en;
       int pos;
    };
    node a[maxn];
    node s;
    struct cmp1{
        bool operator () (node &a,node &b){
            return a.en>b.en; //最小值优先
        }
    };
    priority_queue<node,vector<node>,cmp1>que; //最小值优先      //按结束时间从小到大存储
    //priority_queue<node>que; //采用默认优先级构造队列
    bool cmp(node x,node y)
     {
         if(x.st!=y.st)return x.st<y.st;
        else  x.en<y.en;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].st,&a[i].en);
            a[i].pos=i;
        }
        sort(a,a+n,cmp);    //按开始时间从小到大排序
        que.push(a[0]);
        int ans=1;
        used[a[0].pos]=1;
        for(int i=1;i<n;i++)
        {
            if(!que.empty()){
                s=que.top();
            if(s.en<a[i].st)    
            {
                used[a[i].pos]=used[s.pos];    //更新编号
                que.pop();
            }
            else
            {
                ans++;     //增加机器台数
                used[a[i].pos]=ans;    //更新编号
            }
            }
            que.push(a[i]);
        }
        printf("%d
    ",ans);
        for(int i=0;i<n;i++)
                printf("%d
    ",used[i]);
      }
        return 0;
    }
  • 相关阅读:
    如何不传入对象就获得某对象的方法---ThreadLocal类
    Linux系统主目录被更改,怎么修改回去?
    tree命令的安装
    Linux命令学习man
    当重载函数的参数是Object和Object数组的时候会发生什么情况!!!
    Linux学习(二)之内核、系统调用、库
    使用puttygen转换OpenSSH SSH2私钥为RSA PEM格式
    docker-compose使用详解
    svn迁移到gitlab
    linux快速启动http端口
  • 原文地址:https://www.cnblogs.com/huangzzz/p/8847794.html
Copyright © 2011-2022 走看看