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

    Billboard

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


    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, 宽度为 w 的黑板   现在有 n 条小道消息 每一个都只占用一行 但是长度不一定    所以我们就需要找到 能写下它的哪一行 然后输出行号   行号要要求最小。也就是尽量写在上面

    这题最简单的思路就是 用一个数组来表示每一行剩余的长度 来判断能不能插入这个消息

    但是一定会超时

    这个时候我们就需要用线段树来优化   用线段树表示这个区间最长的剩余长度 

    #include<iostream>
    #include<cmath>
    #include<stdio.h>
    using namespace std;
    int h,w,n,m;
    struct node
    {
        int l,r,the_rest; //保存这个区间的 最大剩余 
    }data[200000*4];
    void built(int l_,int r_,int i)
    {
        data[i].l=l_; data[i].r=r_;
        if(l_==r_){
            data[i].the_rest=w; return ;
        }
        else{
            int mid=(l_+r_)/2;
            built(l_,mid,i*2);
            built(mid+1,r_,i*2+1);
            data[i].the_rest=max(data[i*2].the_rest,data[i*2+1].the_rest);
        }
    }
    
    int insert(int len,int i)  //开始自己想的是 用一个flag 来标记是否已经找到可以插入 来避免后面的多次插入
                                //但是后面看了下别人的代码发现 还是这种好  左子树没有找到再找右子树    
    {
        if(data[i].the_rest<len) return -1;
        if(data[i].l==data[i].r&&data[i].the_rest>=len)
        {
              data[i].the_rest-=len; return data[i].l; 
        }
        int a=insert(len,i*2);
        if(a==-1){
            a=insert(len,i*2+1);
        } 
        data[i].the_rest=max(data[i*2].the_rest,data[i*2+1].the_rest);
        return a;
    }
    int main()
    {
        while(~scanf("%d%d%d",&h,&w,&n))
        {
            built(1,min(h,n),1);
            while(n--)
            {
                scanf("%d",&m);
                cout<<insert(m,1)<<endl;
            }
        }
        return 0;
     } 
  • 相关阅读:
    事件
    10- JMeter5.1.1 工具快速入门
    06- Linux Ubuntu下sublime下载与使用与安装包
    控件是什么意思?
    09- 性能测试关键指标
    08- Tomcat入门与环境搭建部署
    07- HTTP协议详解及Fiddler抓包
    06- web兼容性测试与web兼容性测试工具
    05- web网站链接测试与XENU工具使用
    04- cookie与缓存技术
  • 原文地址:https://www.cnblogs.com/Swust-lyon/p/6702303.html
Copyright © 2011-2022 走看看