hdoj2795 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.
Sample Input
3 5 5
2
4
3
3
3
Sample Output
1
2
1
3
-1

题意:有一个高h,宽w的广告牌,现在有n张广告需要贴在上面,每张广告的高度为1,宽度为输入值,贴广告原则:优先靠近顶部,优先靠近左边。问给定宽度的广告贴在第几行(贴不上则为-1)。

一开始我怎么也想不到这道题可以用线段树,看了网上的博客之后才知道这道题也可以用线段树。

由于每张广告的高度固定且都为1,所以就相当于有h个单位长度为1的区间。但h的长度最大可以达到10的9次方,而线段树需要h*4的数组,显然不可能。但是这道题n最多也就200000,因此h最多也只有200000有用,因此可以建成线段树。每行的宽度为维护对象。

参考代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
const int MAXV = 200000+10;
using namespace std;
struct node {
    int left,right;
    int maxi;
} e[MAXV * 4];
void init() {
    memset(e,0,sizeof(e));
}
int h,w,n;
void push_up(int p) {
    e[p].maxi = max(e[p*2].maxi, e[p*2+1].maxi);
}
void build(int p,int l,int r) {
    e[p].left = l;
    e[p].right = r;
    if (l == r) {
        e[p].maxi = w;//最开始每个区间都没有贴广告(剩余宽度为w);
    }
    else {
        int mid = (l + r) / 2;//建立线段树;
        build(p*2,l,mid);
        build(p*2+1,mid+1,r);
        push_up(p);
    }
}
int query(int p,int l,int r,int wi) {
    int ans;
    if (e[p].maxi < wi) {//这张广告已经贴不下;
        return -1;
    }
    if (l == r) {//找到了所在的行;
        e[p].maxi = e[p].maxi - wi;//该行的剩余宽度;
        return l;
    }
    int mid = (l + r) / 2;
    ans = query(p*2,l,mid,wi);//寻找是否还有位置(先左后右原则);
    if (ans == -1) {
        ans = query(p*2+1,mid+1,r,wi);
    }
    push_up(p);//维护最大值;
    return ans;
}
int main() {
    while(scanf("%d%d%d", &h, &w, &n) != EOF) {
        init();
        int wi;
        if (h > n) h = n;
        build(1,1,h);
        for (int i = 1;i <= n;++i) {
            scanf("%d", &wi);
            int ans = query(1,1,h,wi);
            printf("%d\n", ans);
        }
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,258评论 0 23
  • 打上了这个80后的标签不是强调这篇文章主要是讲80后的事,更加不只是给80后的朋友看的,只是想真正的让大家感受下这...
    卢保林阅读 288评论 0 0
  • 姐姐去参加国学冬令营了,我们表示很想念。弟弟早晨对我说:“妈妈我都要想死姐姐了,都想傻了!”一大早起来就要吃...
    贺清阅读 556评论 0 0
  • 最近接触到了两个概念:直接融资和间接融资。我并没有认真研究这两个概念,我直觉的理解是:直接融资就是需要钱的企业,找...
    Lcww阅读 341评论 0 0
  • 腐 神象镇地狱 地藏度恶鬼 青莲生于淤泥 濯其污秽未污心 凌乱的房间 缭绕的烟雾 糜烂的生活 以及堕落的我 双亲满...
    旅翼阅读 118评论 0 0