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;
     } 
  • 相关阅读:
    响应式卡片抽奖插件 CardShow
    Day10-微信小程序实战-交友小程序-自定义callPhone 和copyText组件
    Day10-微信小程序实战-交友小程序-对点赞次数的优化及首页推荐与最新功能实现-详情页渲染数据和样式布局
    Day8-微信小程序实战-交友小程序-首页用户列表渲染及多账号调试及其点赞功能的实现
    Day8-微信小程序实战-交友小程序-头像的设置
    Day8-微信小程序实战-交友小程序-创建编辑子页和修改个人信息页面
    Day7-微笑小程序实战-交友小程序-登陆模块
    Day7-微笑小程序-交友小程序之“我的”页面-创立用户数据库
    Day7-微信小程序实战-交友小程序首页UI
    Day7-微信小程序实战-交友小程序-tabbar实现
  • 原文地址:https://www.cnblogs.com/Swust-lyon/p/6702303.html
Copyright © 2011-2022 走看看