• Greedy:Stall Reservations(POJ 3190)


                      

                      牛挤奶

      题目大意:一群牛很挑剔,他们仅在一个时间段内挤奶,而且只能在一个棚里面挤,不能与其他牛共享地方,现在给你一群牛,问你如果要全部牛都挤奶,至少需要多少牛棚?

      这一题如果把时间区间去掉,那就变成装箱子问题了(装箱子要用Splay维护),但是现在规定了区间和时间,我们只用贪婪算法就可以了,每次只用找到最小就可以了(用堆维护)。

      PS:一开始我想到用AVL去维护,的都不知道我在想什么,简直浪费时间

      

      1 #include <iostream>
      2 #include <functional>
      3 #include <algorithm>
      4 
      5 using namespace std;
      6 
      7 typedef struct iterval
      8 {
      9     int cows_num;
     10     int Which_Stall;
     11     int start;
     12     int end;
     13 }Cow;
     14 typedef struct box
     15 {
     16     int box_num;
     17     int min_t;
     18 }Stall;
     19 typedef int Position;
     20 int fcomp1(const void *a, const void *b)
     21 {
     22     return (*(Cow *)a).start - (*(Cow *)b).start;
     23 }
     24 int fcomp2(const void *a, const void *b)
     25 {
     26     return (*(Cow *)a).cows_num - (*(Cow *)b).cows_num;
     27 }
     28 
     29 static Cow cows_set[50000];
     30 static Stall s_heap[50001];
     31 static int sum_of_stalls;
     32 
     33 void Search(const int);
     34 void Insert(Stall, Position);
     35 Stall Delete_Min(void);
     36 
     37 int main(void)//这一题用堆维护
     38 {
     39     int n;
     40     while (~scanf("%d", &n))
     41     {
     42         for (int i = 0; i < n; i++)
     43         {
     44             scanf("%d%d", &cows_set[i].start, &cows_set[i].end);
     45             cows_set[i].cows_num = i;
     46         }
     47         qsort(cows_set, n, sizeof(Cow), fcomp1);
     48         Search(n);
     49     }
     50     return 0;
     51 }
     52 
     53 void Insert(Stall goal, Position pos)
     54 {
     55     Position s = pos, pr;
     56 
     57     for (; s > 1; s = pr)//上滤
     58     {
     59         pr = s % 2 == 0 ? s >> 1 : (s - 1) >> 1;
     60         if (s_heap[pr].min_t > goal.min_t)
     61             s_heap[s] = s_heap[pr];
     62         else break;
     63     }
     64     s_heap[s] = goal;
     65 }
     66 
     67 Stall Delete_Min(void)
     68 {
     69     Stall mins_stalls = s_heap[1],tmp = s_heap[sum_of_stalls--];
     70     Position pr, s1, s2;
     71 
     72     for (pr = 1; pr <= sum_of_stalls;)
     73     {
     74         s1 = pr << 1; s2 = s1 + 1;
     75         if (s2 <= sum_of_stalls)
     76         {
     77             if (s_heap[s1].min_t < s_heap[s2].min_t){
     78                 s_heap[pr] = s_heap[s1]; pr = s1;
     79             }
     80             else{
     81                 s_heap[pr] = s_heap[s2]; pr = s2;
     82             }
     83         }
     84         else
     85         {
     86             if (s1 <= sum_of_stalls){
     87                 s_heap[pr] = s_heap[s1]; pr = s1;
     88             }
     89             break;
     90         }
     91     }
     92     Insert(tmp, pr);
     93     return mins_stalls;
     94 }
     95 
     96 void Search(const int n)
     97 {
     98     Stall tmp;
     99     sum_of_stalls = 1;
    100     tmp.box_num = 1; tmp.min_t = cows_set[0].end;
    101     Insert(tmp, 1);
    102     cows_set[0].Which_Stall = 1; 
    103 
    104     for (int i = 1; i < n; i++)
    105     {
    106         if (cows_set[i].start <= s_heap[1].min_t)//放不下
    107         {
    108             tmp.box_num = ++sum_of_stalls; tmp.min_t = cows_set[i].end;
    109             Insert(tmp, sum_of_stalls);
    110             cows_set[i].Which_Stall = sum_of_stalls;
    111         }
    112         else
    113         {
    114             tmp = Delete_Min();
    115             tmp.min_t = cows_set[i].end;
    116             cows_set[i].Which_Stall = tmp.box_num;
    117             Insert(tmp, ++sum_of_stalls);
    118         }
    119     }
    120     printf("%d
    ", sum_of_stalls);
    121     qsort(cows_set, n, sizeof(Cow), fcomp2);
    122     for (int i = 0; i < n; i++)
    123         printf("%d
    ", cows_set[i].Which_Stall);
    124 }

  • 相关阅读:
    PHP Fatal error: Uncaught Error: Call to undefined function pcntl_fork().. 开启php pcntl扩展实现多进程
    php实现共享内存进程通信函数之_shm
    Centos下10000次循环测试php对Redis和共享内存(shm)读写效率
    php操作共享内存shmop类及简单使用测试(代码)
    作为phper既然了解共享内存函数shmop的使用方法,那么就必须要了解一下信号量是什么,以及信号量使用的代码案例
    php简单使用shmop函数创建共享内存减少服务器负载
    作为php了解一下共享内存的概念及优缺点
    给PHP开启shmop扩展实现共享内存
    Centos环境自写脚本查看使用php或nginx占用内存
    linux下查看和添加path环境变量
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/4885011.html
Copyright © 2020-2023  润新知