HDU 2795 Billboard(宣传栏贴公告,线段树应用)

HDU 2795 Billboard(宣传栏贴公告,线段树应用)

ACM

题目地址:HDU 2795 Billboard

题意:

要在h*w宣传栏上贴公告,每条公告的高度都是为1的,而且每条公告都要尽量贴最上面最靠左边的,给你一系列的公告的长度,问它们能不能贴上。

分析:

不是很好想,不过想到了就很好写了。

只要把宣传栏倒过来就好办了,这时候就是变成有h条位置可以填公告,填放公告时就可以尽量找最左边的合适的位置来放了。

可以用线段树实现,查找的复杂度是O(logn),需要注意的坑点是h的范围非常大,如果真的那么大线段树是开不下去的,但是n的范围才20w,而即使所有公告都要占一栏也不会超过n,所以线段树开min(h, n)就行了。

代码:

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        2795.cpp
*  Create Date: 2014-08-05 16:12:47
*  Descripton:  segment tree
*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
#define repf(i,a,b) for(int i=(a);i<=(b);i++)

#define lson(x) ((x) << 1)
#define rson(x) ((x) << 1 | 1)

typedef long long ll;

const int N = 200010;
const int ROOT = 1;

int h, w, n, t;

// below is sement point updated version
struct seg {
    ll w;
};

struct segment_tree {
    seg node[N << 2];

    void update(int pos) {
        node[pos].w = max(node[lson(pos)].w, node[rson(pos)].w);
    }

    void build(int l, int r, int pos) {
        if (l == r) {
            node[pos].w = w;
            return;
        }
        int m = (l + r) >> 1;
        build(l, m, lson(pos));
        build(m + 1, r, rson(pos));
        update(pos);
    }

    int queryandmodify(int l, int r, int pos, ll y) {
        if (y > node[pos].w) {
            return -1;
        }
        if (l == r) {
            node[pos].w -= y;
            return l;
        }
        int m = (l + r) >> 1;
        int res;
        if (y <= node[lson(pos)].w)
            res = queryandmodify(l, m, lson(pos), y);
        else
            res = queryandmodify(m + 1, r, rson(pos), y);
        update(pos);
        return res;
    }

} sgm;

int main() {
    while (~scanf("%d%d%d", &h, &w, &n)) {
        h = min(h, n);
        sgm.build(1, h, ROOT);
        repf (i, 1, n) {
            scanf("%d", &t);
            printf("%d\n", sgm.queryandmodify(1, h, ROOT, t));
        }
    }
    return 0;
}

HDU 2795 Billboard(宣传栏贴公告,线段树应用),布布扣,bubuko.com

时间: 2024-11-29 01:27:49

HDU 2795 Billboard(宣传栏贴公告,线段树应用)的相关文章

HDU 2795 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, chang

hdu 2795 Billboard(线段树)

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10890    Accepted Submission(s): 4827 Problem Description At the entrance to the university, there is a huge rectangular billboard of

【线段树四】HDU 2795 Billboard

BillboardTime Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9045    Accepted Submission(s): 4021 Problem Description At the entrance to the university, there is a huge rectangular billboard of siz

HDU 2795 Billboard (线段树单点更新)

题意:h,w,n:有一个h*w尺寸的木板,n张1*wi的海报,贴海报的位置尽量高,尽量往左,问每张海报贴的高度 看到1 <= h,w <= 10^9; 1 <= n <= 200,000,应该就是线段树了. 关键在怎么建树,这里我们对h进行分割,每个高度都有等长的w,我们从上往下贴,如果当前高度 (在同一高度上l==r)的长度可以满足wi则可以贴,否则继续往下寻找. #include <iostream> #include <stdio.h> #includ

HDU 2795 Billboard (RE的可以看一看)

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, chang

hdu 1754:I Hate It(线段树,入门题,RMQ问题)

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 33726    Accepted Submission(s): 13266 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的要求

hdu 1556:Color the ball(线段树,区间更新,经典题)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7941    Accepted Submission(s): 4070 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电

hdu 1698:Just a Hook(线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15129    Accepted Submission(s): 7506 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing f

hdu 1394 Minimum Inversion Number(线段树)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10853    Accepted Submission(s): 6676 Problem Description The inversion number of a given number sequence a1, a2, ..., a