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

    题目:

    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


    思路:

    • 一开始以为这题跟我以前做的一道求最大重叠区间数一样,然后发现要求每头牛安排在哪个棚就不能按那题的方法;
    • 我用当时做的比较笨的方法一个个棚求可以合并的牛,过了样例,但由于区间范围比那题大很多所以不出所料TLE;
    • 一开始想用离散化优化没写出来,后来看了别人的博客才知道用优先队列优化更方便。


    Code:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<queue>
     6 
     7 using namespace std;
     8 const int MAXN = 50010;
     9 int cnt[MAXN];
    10 
    11 struct node {
    12     int l, r, id;
    13     friend bool operator < (node a, node b) {
    14         return a.r > b.r;
    15     }
    16 }a[MAXN];
    17 
    18 bool cmp(node a, node b) {
    19     if (a.l == b.l) return a.r < b.r;
    20     return a.l < b.l;
    21 }
    22 
    23 priority_queue<node> q;
    24 
    25 int main() {
    26     std::ios::sync_with_stdio(false);
    27     int n;
    28     while(cin>>n) {
    29         for (int i = 0; i < n; ++i) {
    30             cin>>a[i].l>>a[i].r;
    31             a[i].id = i;
    32         }
    33         sort(a, a+n, cmp);
    34         int ans = 0;
    35         for (int i = 0; i < n; ++i) {
    36             if (!q.empty() && q.top().r < a[i].l) {
    37                 cnt[a[i].id] = cnt[q.top().id];
    38                 q.pop();
    39             }
    40             else cnt[a[i].id] = ++ans;
    41             q.push(a[i]);
    42         }
    43         cout<<ans<<endl;
    44         for (int i = 0; i < n; ++i) cout<<cnt[i]<<endl;
    45     }
    46 
    47     return 0;
    48 }
  • 相关阅读:
    java得到当前日期的前一天或后一天
    java通过年月得到该月每一天的日期
    前后端分离的跨域请求问题解决
    关于java中分割字符串
    Linux终端命令
    eclipse中tomcat可以start启动,无法debug启动的解决
    通过DOS界面查看电脑上端口使用情况
    前端css
    mysql数据库
    IO阻塞
  • 原文地址:https://www.cnblogs.com/robin1998/p/6360370.html
Copyright © 2011-2022 走看看