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

    Problem Description
    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.
     
    Input
    There 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.
     
    Output
    For 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.
     
    题意:给你一个高为h,宽为w的告示牌,总共有n个广告。没个广告高1宽wi,尽量将广告向左上方贴,如果宽度大于w那么智能换行贴。
    问你每个广告贴的高度是多少。
     
    用线段树模拟,要是左子树的最大值比当前广告大,就查询更新左子树,否则就右子树。
     
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int M = 2e5 + 10;
    struct TnT {
        int l , r , max;
    }T[M << 2];
    int h , w , n , ans;
    void build(int l , int r , int p) {
        int mid = (l + r) >> 1;
        T[p].l = l , T[p].r = r , T[p].max = w;
        if(T[p].l == T[p].r) {
            return ;
        }
        build(l , mid , p << 1);
        build(mid + 1 , r , (p << 1) | 1);
    }
    void query(int p , int c) {
        if(T[p].l == T[p].r) {
            T[p].max -= c;
            ans = T[p].l;
            return ;
        }
        if(c <= T[p << 1].max) {
            query(p << 1 , c);
        }
        else {
            query((p << 1) | 1 , c);
        }
        T[p].max = max(T[p << 1].max , T[(p << 1) | 1].max);
    }
    
    int main()
    {
        while(scanf("%d%d%d" , &h , &w , &n) != EOF) {
            h = min(h , n);
            build(1 , h , 1);
            for(int i = 1 ; i <= n ; i++) {
                int x;
                scanf("%d" , &x);
                if(T[1].max < x) {
                    printf("-1
    ");
                }
                else {
                    query(1 , x);
                    printf("%d
    " , ans);
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    linux基础学习-6.4-Linux无法上网排查流程
    [JSOI2007][BZOJ1029] 建筑抢修
    [HNOI2003][BZOJ1216] 操作系统
    [Apio2009][BZOJ1179] Atm
    [Tjoi2013][BZOJ3172] 单词
    AC自动机学习笔记
    [转]一个比较通俗的KMP算法讲解
    [HAOI2008][BZOJ1042] 硬币购物
    [NOI2007][BZOJ1491] 社交网络
    [SCOI2009][BZOJ1295] 最长距离
  • 原文地址:https://www.cnblogs.com/TnT2333333/p/6013807.html
Copyright © 2011-2022 走看看