• poj3190区间类贪心+优先队列


    题意:每个奶牛产奶的时间为A到B,每个奶牛产奶时要占用一间房子,问n头奶牛产奶共需要多少房子,并输出每头奶牛用哪间房子

    分析:这题就是一个裸的贪心,将奶牛按开始时间进行排序即可,但考虑一下数据范围,我们可以用一个优先队列来进行维护,在优先队列中我们按照奶牛的结束时间最小构造小顶堆,然后判断新进来的元素的开始时间是否比最小的结束时间大,若是,加入队列,修改队列,若不是,加入对列,并且计数加1

    注意自定义类型的优先队列的维护方法。详见:http://blog.sina.com.cn/s/blog_4e5157120100vn7b.html

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <string>
     5 #include <vector>
     6 #include <algorithm>
     7 #include <set>
     8 #include <map>
     9 #include <bitset>
    10 #include <cmath>
    11 #include <queue>
    12 #include <stack>
    13 using namespace std;
    14 const int maxn=50050;
    15 //注意自定义类型在优先队列中的用法
    16 typedef struct P
    17 {
    18     int start,over,id;
    19     friend bool operator<(P a,P b)
    20     {
    21         return a.over>b.over; //小顶堆
    22     }
    23 }P;
    24 P point[maxn];
    25 bool cmp(P a,P b)
    26 {
    27     return a.start<b.start;
    28 }
    29 int t[maxn];
    30 int main()
    31 {
    32     int n;
    33     while(cin>>n)
    34     {
    35         for(int i=0;i<n;i++)
    36         {
    37             scanf("%d%d",&point[i].start,&point[i].over);
    38             point[i].id=i;
    39         }
    40         memset(t,0,sizeof(t));
    41         sort(point,point+n,cmp);
    42         priority_queue<P> que;
    43         int r=0; //统计个数
    44         que.push(point[0]);
    45         t[point[0].id]=++r;
    46         for(int i=1;i<n;i++)
    47         {
    48             P node=que.top();
    49             if(node.over<point[i].start){
    50                 t[point[i].id]=t[node.id];
    51                 que.pop();
    52                 que.push(point[i]);
    53             }
    54             else{
    55                 t[point[i].id]=++r;
    56                 que.push(point[i]);
    57             }
    58         }
    59         cout<<r<<endl;
    60         for(int i=0;i<n;i++)
    61             printf("%d
    ",t[i]);
    62     }
    63     return 0;
    64 }
    View Code
  • 相关阅读:
    10.11-10.16
    10.8-10.10
    9.26-28
    9.29css继承属性
    表单的学习
    排版
    css补充
    今天学的新内容
    新内容
    文本样式的修改
  • 原文地址:https://www.cnblogs.com/wolf940509/p/5326647.html
Copyright © 2020-2023  润新知