zoukankan      html  css  js  c++  java
  • hdu 2795 Billboard 线段树+二分

    Billboard

    Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


    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
     
    Author
    hhanger@zju
     
    Source
    思路:查找区间最大值,二分整个区间,找到最小大于等于询问的数,更新节点;
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    #define true ture
    #define false flase
    using namespace std;
    #define ll long long
    int scan()
    {
        int res = 0 , ch ;
        while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
        {
            if( ch == EOF )  return 1 << 30 ;
        }
        res = ch - '0' ;
        while( ( ch = getchar() ) >= '0' && ch <= '9' )
            res = res * 10 + ( ch - '0' ) ;
        return res ;
    }
    int w;
    struct is
    {
        int l,r;
        int num;
    }tree[200010*3];
    void build_tree(int l,int r,int pos)
    {
        tree[pos].l=l;
        tree[pos].r=r;
        if(l==r)
        {
            tree[pos].num=w;
            //scanf("%lld",&tree[pos].num);
            return;
        }
        int mid=(l+r)/2;
        build_tree(l,mid,pos*2);
        build_tree(mid+1,r,pos*2+1);
        tree[pos].num=max(tree[pos*2].num,tree[pos*2+1].num);
    }
    void update(int l,int r,int change,int pos)
    {
        if(tree[pos].l==l&&tree[pos].r==r)
        {
            tree[pos].num+=change;
            return;
        }
        int mid=(tree[pos].l+tree[pos].r)/2;
        if(r<=mid)
        update(l,r,change,pos*2);
        else if(l>mid)
        update(l,r,change,pos*2+1);
        else
        {
            update(l,mid,change,pos*2);
            update(mid+1,r,change,pos*2+1);
        }
        tree[pos].num=max(tree[pos*2].num,tree[pos*2+1].num);
    }
    int query(int l,int r,int pos)
    {
        //cout<<l<<" "<<r<<" "<<pos<<endl;
        if(tree[pos].l==l&&tree[pos].r==r)
        return tree[pos].num;
        int mid=(tree[pos].l+tree[pos].r)/2;
        if(l>mid)
        return query(l,r,pos*2+1);
        else if(r<=mid)
        return query(l,r,pos*2);
        else
        return max(query(l,mid,pos*2),query(mid+1,r,pos*2+1));
    }
    int main()
    {
        int x,q,i,t;
        while(~scanf("%d%d%d",&x,&w,&q))
        {
            if(x>q)
                x=q;
            build_tree(1,x,1);
            while(q--)
            {
                int fi;
                scanf("%d",&fi);
                int st=1;
                int en=x;
                int mid=(st+en)>>1;
                if(query(st,en,1)<fi)
                printf("-1
    ");
                else
                {
                    while(st<en)
                    {
                        mid=(st+en)>>1;
                        int ans=query(st,mid,1);
                        if(ans>=fi)
                        en=mid;
                        else
                        st=mid+1;
                    }
                    printf("%d
    ",st);
                    update(st,st,-fi,1);
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    spring aop实现数据库的读写分离
    MySQL主从复制(Master-Slave)实践
    java 注解 Annontation
    java NIO介绍
    为什么你学不会递归?告别递归,谈谈我的一些经验(转)
    maven的安装、路径配置、修改库文件路径和eclipse中的配置、创建maven工程(转)
    Eclipse中创建Maven多模块工程
    Eclipse的Working Set管理项目
    Java使用POI读取和写入Excel指南(转)
    Webpack安装和命令
  • 原文地址:https://www.cnblogs.com/jhz033/p/5400533.html
Copyright © 2011-2022 走看看