zoukankan      html  css  js  c++  java
  • HDU 2795 Billboard 线段树,区间最大值,单点更新

    Billboard

    Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 21203    Accepted Submission(s): 8750


    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.
     

    Sample Input
    3 5 5 2 4 3 3 3
     

    Sample Output
    1 2 1 3 -1
     

    思路:

    刚开始是按照宽度去想的,每一个宽度里面的各自最大数目是h。然后一边一边刷,开数组就需要1e9<<2。果断放不下。然后按照h去考虑,相当于求区间最大值。但是h最大取到1e9,数组还是需要开1e9<<2。瞬间不知道怎么办。讨论区里面有人说不用开那么大,太大无意义。重新看题。题目上约束了n的范围,200000。假如最理想的情况我们每一行放置一个广告,最多需要n行就可以,如果给定的h比这个数值要大,那么有h-n行就当于浪费空间。所以,线段树的大小就是min(h,n)<<2。

    刚开始用的不是区间最大值,用的还是区间求和,然后过了样例,但是想法是错误,因为有一些情况他仍旧考虑不到。比如下面的图片:

    假如我现在插入宽度为5的,那么按照sum区间和的情况肯定往左边走,然后就会返回-1。而实际上,右子树就可以放置下。

    更新直接在查询的时候顺带完成

    代码:

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<string>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    const int maxn=200005;
    long long maxnum[maxn<<2];
    int h,w,n;
    void pushup(int rt) {
        maxnum[rt]=max(maxnum[rt<<1],maxnum[rt<<1|1]);
    }
    void build(int l, int r, int rt) {
        maxnum[rt]=w;
        if(l==r) return;
        int mid=(l+r)>>1;
        build(lson);
        build(rson);
        pushup(rt);
    }
    int query(int width, int l, int r, int rt) {
        if(l==r) {
            maxnum[rt]-=width;
            return l;
        }
        int mid=(l+r)>>1,pos;
        if(maxnum[rt<<1]>=width) pos=query(width,lson);
        else pos=query(width,rson);
        pushup(rt);
        return pos;
    }
    int main() {
        while(~scanf("%d%d%d",&h,&w,&n)) {
            h=min(h,n);
            build(1,h,1);
            for(int i=1;i<=n;++i) {
                int width,pos;scanf("%d",&width);
                if(maxnum[1]<width) pos=-1;
                else pos=query(width,1,h,1);
                printf("%d
    ",pos);
            }
        }
        return 0;
    }
    


  • 相关阅读:
    java中map接口hashMap以及Enty之间的用法和关系
    Collection集合总结,List和set集合的用法,HashSet和LinkedHashSetde用法
    微信小程序开发攻略
    Java闰年的计算,Calendar的用法
    Java计算计算活了多少天
    Java用代码演示String类中的以下方法的用法
    Java判断一个字符串中有多少大写字母、小写字母和数字
    Java将一个字符串的首位改为大写后边改为小写的实现,String
    Java 获取一个字符串中,另一个字符串出现的次数
    linux下重命名文件或文件夹(linux)
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7775998.html
Copyright © 2011-2022 走看看