zoukankan      html  css  js  c++  java
  • Billboard(线段树)

    题目描述:

    自己看去吧~

    At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.

    On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.

    Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.

    When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.

    If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).

    Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.

    InputThere are multiple cases (no more than 40 cases).

    The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.

    Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.OutputFor each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.

    Solution

    注意到它每次都会放到最左边能放的地方,所以每行空余的格子一定是这一行的后缀,我们只用记录每一行有多少空余格子即可。

    而我们要找到第一个满足要求的地方(也就是空格子大于wi的地方),我们可以记录前缀max,然后二分。

    珂惜这样做的复杂度是不对的。我们可以用线段树来维护前缀max,单次查找复杂度logn。

    然后再来个单点修改即可。

    然后我们发现h<=10^9,其实这是虚大,最坏情况每行放一个,也就是n行,所以线段树下标只用建到n即可。

    #include <iostream>
    #include <cstdio>
    using namespace std;
    const int N = 200010;
    struct node{
        int maxi;
    }tr[N << 2];
    int h, w, n, k, ans;
    void build(int p, int l, int r) {
        if (l == r) {
            tr[p].maxi = w;
            return;
        }
        int mid = (l + r) >> 1;
        build(p << 1, l, mid);
        build(p << 1 | 1, mid + 1, r);
        tr[p].maxi = max(tr[p << 1].maxi, tr[p << 1 | 1].maxi);
    }
    void work(int p, int l, int r, int v) {
        if (l == r) {
            tr[p].maxi -= v;
            ans = l;
            return;
        }
        int mid = (l + r) >> 1;
        if (tr[p << 1].maxi >= v) work(p << 1, l, mid, v);
        else work(p << 1 | 1, mid + 1, r, v);
        tr[p].maxi = max(tr[p << 1].maxi, tr[p << 1 | 1].maxi);
    }
    int main() {
        while (scanf("%d%d%d", &h, &w, &n) != EOF) {
            h = min(h, n);
            build(1, 1, h);
            while (n--) {
                scanf("%d", &k);
                if (tr[1].maxi < k) {
                    printf("-1
    ");
                } else {
                    work(1, 1, h, k);
                    printf("%d
    ", ans);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    HDU4652 Dice
    CF113D Museum / BZOJ3270 博物馆
    SHOI2013 超级跳马
    最基本的卷积与反演
    NOI2014 动物园题解
    SP11414 COT3
    new to do
    linux C++中宏定义的问题:error: unable to find string literal operator ‘operator""fmt’ with ‘const char [4]’, ‘long unsigned int’ arguments
    新装vs2010的问题:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
    windows下删除虚拟串口的方法,以及解决串口使用中,无法变更设备串口号的问题
  • 原文地址:https://www.cnblogs.com/zcr-blog/p/12738604.html
Copyright © 2011-2022 走看看