题目链接

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,宽度是wi,每次从最上最左的空位写,假设有空位输出第几行。假设没有足够空位输出-1。

分析:

用高度来构建线段树,记录left行到right行中的最大留白空间,然后每贴一个广告,就在线段树中插,如果左子树的空位长度大于当前要贴的广告长度,就先查找左子树,(因为题目要求贴最左上方),当找到贴的位置,则该行的新的剩余长度就在原值上减去当前长度。并更新整棵线段树。

代码:

#include <iostream>
#include <stdio.h>
#define lchild left,mid,root<<1
#define rchild mid+1,right,root<<1|1
using namespace std;
const int maxn=1e8;
int Max[maxn<<2]; ///用来存放某些行区间所能存放的最大值
int height,width,n; ///构建线段树
void push_up(int root)
{
Max[root] = max(Max[root<<1],Max[root<<1|1]);
} void build(int left,int right,int root)
{
Max[root] = width; ///初始,每个区间所剩余的最大长度都为width
if(left == right) return;
int mid = (left+right)>>1;
///递归构建左右子树
build(lchild);
build(rchild);
} ///查询并插入
int query(int w,int left,int right,int root)
{
if(left == right) ///则将该板插入到这一行,这一节点所剩余的宽度减去w
{
Max[root] -= w;
return left; ///返回木板所插入的行
}
int mid = (left+right)>>1;
int ans=0;
if(w<=Max[root<<1]) ///如果广告的宽度小于左区间的最大宽度,题目要求往左区间靠
ans = query(w,lchild);
else
ans = query(w,rchild);
push_up(root);
return ans;
} int main()
{
while(~scanf("%d%d%d",&height,&width,&n))
{
if(n<height) ///所要贴的广告数小于高度,则线段树按n建,否则按height建立
{
height = n;
}
build(1,height,1); ///构建线段树
int w;
while(n--)
{
scanf("%d",&w);
if(Max[1]<w)
printf("-1\n");
else
{
int ans = query(w,1,height,1);
printf("%d\n",ans);
}
}
}
return 0;
}

Billboard HDU 2795 (线段树)的更多相关文章

  1. hdu 2795 线段树(二维问题一维化)

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. HDU 2795 线段树单点更新

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 2795 线段树区间最大值,单点更新+二分

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. hdu 2795 线段树(纵向)

    注意h的范围和n的范围,纵向建立线段树 题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子思路:每次找到最大值的位子,然后减去L线段树功能:query:区间求最大值的位子(直接 ...

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

    h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子. 每次找能放纸条而且是最上面的位置,询问完以后可以同时更新,所以可以把update和query写在同一个函数里. #include ...

  6. hdu 2795 线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 #include <cstdio> #include <cmath> # ...

  7. Billboard HDU - 2795(树状数组,单点修改,区间查询)

    题目链接:https://vjudge.net/problem/HDU-2795 思路:h = 1e9行不通,因为广告是1*w的,所以n个广告最多只需要 h = n的高度,那么h=2e5就可以接受了. ...

  8. hdu 2795线段树

    #include<stdio.h> #define N 200005 int h,w,n; struct node { int x,y,max; }a]; int mmax(int e,i ...

  9. Billboard (HDU 2795)

    Billboard (HDU 2795) Hdu 2795 注意到每个广告的长度是1,因此可以将每这一张广告牌当成一个数列表示,每个初始值为w.使用线段树维护这个数列,每次查询为找到这个数列第一个大于 ...

  10. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

随机推荐

  1. Web界面和Winform界面生成,代码生成工具

    在上面一篇随笔<代码生成工具之界面快速生成>介绍了代码生成工具Database2Sharp的界面生成操作,其中介绍了Web界面(包括列表界面.内容显示.内容编辑界面的生成,另外还介绍了Wi ...

  2. c++的一些编程技巧和细节

    1.函数形参,如: CreateProcess(                  NULL,                  cmdbuf,                  NULL,      ...

  3. bzoj3864-hdu4899-Hero meet devil

    题目 给出一个由AGTC组成的字符串\(S\),长度为\(n\),对于每个\(i\in [0,n]\),问有多少个长度为\(m\),仅含有AGTC的字符串\(T\)使得\(S\)与\(T\)的最长公共 ...

  4. 【bzoj3545/bzoj3551】[ONTAK2010]Peaks/加强版 Kruskal+树上倍增+Dfs序+主席树

    bzoj3545 题目描述 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询 ...

  5. 通过logger命令记录日志

    通过logger命令记录日志 logger是一个shell命令接口,可以通过该接口使用Syslog的系统日志模块,还可以从命令行直接向系统日志文件写入一行信息. ------------------- ...

  6. 51NOD 1594:Gcd and Phi——题解

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1594 参考及详细推导:http://www.cnblogs.com/ri ...

  7. POJ.1287 Networking (Prim)

    POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...

  8. UVA.674 Coin Change (DP 完全背包)

    UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...

  9. 使用 XHProf 分析你的 PHP 程序

     个人说明: 注意: php5.5运行 xhprof_enable 会发生段错误Segmentation fault: ,这是一个已知的bug. 下面是报错测试脚本: function loader( ...

  10. LVM分区

    使用LVM对磁盘进行初始化 pvcreate /dev/vdd 创建卷组 vgcreate vg /dev/vdd 备注:vg是卷组的名称,可改变. 查看卷组的详细信息 vgdisplay 下图是我执 ...