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

    http://poj.org/problem?id=3190

    //最初思路 按数组排序 也好 堆维护也好 都是想 让开始时间 > 结束时间的牛合并 然后最后求出 个数就好
    //但是这样无法求得每头牛的分配过程
    //所以 模拟 加贪心 堆模拟正在挤奶的机器 按结束时间从小到大排列 没搜索加入一头牛 就结束一头牛的挤奶
    //这样可以求得每头牛 在哪个机器挤奶 并且如果 新加入牛时 这头牛已经结束 那么它就可以接着使用 否则 只能新加一台机器
    //然后stall就是 进一头牛 确定一个的stall 这样就是纯模拟这个个过程
    //一道贪心、模拟的好题

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 #include <queue>
     5 
     6 using namespace std;
     7 
     8 int N;
     9 int stall[50003];
    10 struct Section
    11 {
    12     int x,y;
    13     int pos;
    14     bool operator < (Section a) const //堆按结束时间升序排列
    15     {
    16         if (y == a.y) return x > a.x;
    17         else return y > a.y;
    18     }
    19 
    20 }section[50003];
    21 
    22 bool cmp(Section a, Section b)//数组按开始时间升序排列
    23 {
    24     if(a.x == b.x) return a.y < b.y;
    25     else return a.x < b.x;
    26 }
    27 
    28 priority_queue<Section> que;
    29 int solve()
    30 {
    31     int ans = 0;
    32     Section node;
    33     for (int i = 0; i < N;i ++)
    34     {
    35         if ( !que.empty() )
    36         {
    37             node = que.top();
    38             if (que.top().y < section[i].x)//如果加进这个section的时候有结束的奶牛-->>>这里错了 不是当结束时间和开始时间相等时不能接替的
    39             {
    40                 stall[section[i].pos] = stall[que.top().pos];
    41                 que.pop();//这头奶牛就可以出去了
    42             }
    43             else
    44             {
    45                 ans++;
    46                 stall[section[i].pos] = ans;
    47             }
    48         }
    49         else
    50         {
    51             ans++;
    52             stall[section[i].pos] = ans;
    53         }
    54         que.push(section[i]);
    55     }
    56     return ans;
    57 }
    58 
    59 int main()
    60 {
    61     freopen("in.txt", "r", stdin);
    62     while (~scanf("%d", &N))
    63     {
    64         while (!que.empty()) que.pop();
    65         for (int i = 0; i < N; i++)
    66         {
    67             scanf("%d%d", &section[i].x, &section[i].y);
    68             section[i].pos = i;
    69             //stall[section[i].pos] = i;//安排最初的stall
    70         }
    71         sort(section, section+N, cmp);//当自定义比较函数时 是可以忽略 结构体内部的操作符重载的
    72         int ans = solve();
    73         printf("%d
    ",ans);
    74         for (int i = 0;i < N; i++)
    75         {
    76             printf("%d
    ", stall[i]);
    77         }
    78     }
    79     return 0;
    80 }
  • 相关阅读:
    2.安装双系统linux+windows
    1.在虚拟机上安装linux系统
    python2升级python3(linux)并解决兼容问题
    selenium模拟登录_百度指数
    解决YUM下LoadedpluginsfastestmirrorDeterminingfastestmirrors 的问题
    yum源地址卸载、下载、安装
    Redis无法启动You may fix this problem by either reducing the size of the Redis heap with the --maxheap
    第二章 演化式架构师
    第一章 微服务
    第19章 未来的软件构架
  • 原文地址:https://www.cnblogs.com/oscar-cnblogs/p/6308993.html
Copyright © 2011-2022 走看看