HDU Billboard

题目分析:给你n张海报,一个宣传板。让你在满足海报可以贴在最高位置的时候则贴的最高,无法满足时贴的最靠左,输出海报所贴的高度。如果不能贴则输出-1.

一道很简单,但是我没想出的基础线段树。

算法思想:

把宣传板的高度转换成线段树的区间,从而得知每一个区间的大小当然为宣传板的宽度啦。然后,每次查询每个区间内的大小是否有满足当前海报宽度的,有责得到结果。然后,在更新减去当前所用的宽度。根据线段是的特点,我们知道我们会先得到小的答案,即,较高的高度(高度从1-h)。

还有一个需要处理的就是,当高度超过200000的时候,要截取。因为海报最多才200000,所以不需要那么高的宣传板。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

#define L(rt) (rt << 1)
#define R(rt) (rt << 1 | 1)
#define MID(a,b) (a+((b-a) >> 1))
#define lson lft,mid,rt << 1
#define rson mid+1,rht,rt << 1 | 1

const int MAXN = 222222;

struct Node{
   int lft,rht,mx;
   int mid(){return MID(lft,rht);};
};

Node tree[MAXN*4];
int w,h,n;
class Segtree{
public:
    void Build(int lft,int rht,int rt){
        tree[rt].lft = lft;tree[rt].rht = rht;
        tree[rt].mx = w;
        if(lft != rht){
            int mid = tree[rt].mid();
            Build(lson);
            Build(rson);
        }
    }
    int Update(int rt,int val){
        int lft = tree[rt].lft,rht = tree[rt].rht;
        if(lft == rht){
            tree[rt].mx -= val;
            return lft;
        }
        else{
            int pos;
            if(tree[L(rt)].mx >= val)pos = Update(L(rt),val);
            else pos = Update(R(rt),val);
            tree[rt].mx = max(tree[L(rt)].mx,tree[R(rt)].mx);
            return pos;
        }
    }
};

int main()
{
    while(~scanf("%d%d%d",&h,&w,&n)){
        Segtree seg;
        seg.Build(1,min(h,MAXN),1);
        for(int i = 0;i < n;++i){
            int wid;
            scanf("%d",&wid);
            if(tree[1].mx < wid)printf("-1\n");
            else printf("%d\n",seg.Update(1,wid));
        }
    }
    return 0;
}

HDU Billboard

时间: 2024-08-15 01:57:12

HDU Billboard的相关文章

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(线段树+单点更新)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13050    Accepted Submission(s): 5651 Problem Description At the entrance to the un

hdu 1882 Strange Billboard(位运算+枚举)

http://acm.hdu.edu.cn/showproblem.php?pid=1882 感觉非常不错的一道题. 给一个n*m(1<=n,m<=16)的矩阵,每一个格子都有黑白两面,当翻一个格子时,它的上下左右都要翻转,问最后使格子全变为白色的最少翻转步数. 仅仅需枚举第一行的状态即可,由于对于第i(i>=2)行j列翻转情况受上一行的制约,仅仅有当上一行也是'X'的时候,该行j列才干翻转,使i-1行j列变为'.',否则i行j列不能翻转.依次进行下去,当最后一行全变为白色,说明翻转成功

HDU 2795 Billboard(线段树啊 )

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 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

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

HDU 2795 Billboard(宣传栏贴公告,线段树应用) ACM 题目地址:HDU 2795 Billboard 题意: 要在h*w宣传栏上贴公告,每条公告的高度都是为1的,而且每条公告都要尽量贴最上面最靠左边的,给你一系列的公告的长度,问它们能不能贴上. 分析: 不是很好想,不过想到了就很好写了. 只要把宣传栏倒过来就好办了,这时候就是变成有h条位置可以填公告,填放公告时就可以尽量找最左边的合适的位置来放了. 可以用线段树实现,查找的复杂度是O(logn),需要注意的坑点是h的范围非常

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 1882 Strange Billboard(位运算)

题目链接 题意 : 给你一个矩阵,有黑有白,翻转一个块可以让上下左右都翻转过来,问最少翻转多少次能让矩阵变为全白. 思路 : 我们从第一行开始枚举要翻转的状态,最多可以枚举到2的16次方,因为你只要第一行的确定了,第二行要翻转的也就确定了,所以第一行的状态决定了最后的状态.看了网上大神,真是让位运算废了啊,,,,,太复杂了...... 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #

HDU 2795:Billboard(线段树)

http://acm.hdu.edu.cn/showproblem.php?pid=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 announce