题目链接

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. 3dContactPointAnnotationTool开发日志(十六)

      调了一上午才发现是把下面这个函数: private float DivideTriangle(int []triangle,out int []outTriangle,List<Vector ...

  2. Android基础------高级ul:消息提示

    前言:Android消息提示笔记,刚刚接触Android 1.静态方法Toast 直接调用静态方法 //消息提示(context,"内容",固定时间) Toast.makeText ...

  3. 创建 cordova 项目

    1. 安装 node.js 2.安装 cordova : npm install -g cordova 3.创建 安卓项目: cordova create <项目路径>  <包名&g ...

  4. 【bzoj1725】[USACO2006 Nov]Corn Fields牧场的安排 状态压缩dp

    题目描述 Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧场上的某几格土 ...

  5. NetScaler + Wireshark = A Perfect Combination!

    NetScaler + Wireshark = A Perfect Combination! https://www.citrix.com/blogs/2014/05/03/netscaler-wir ...

  6. [HZOI2016]偏序&[HZOI2015]偏序II K维偏序问题

    description Cogs: [HZOI2016]偏序 [HZOI2015]偏序 II data range \[ n\le 5\times 10^4\] solution 嵌套\(CDQ\)的 ...

  7. 「CodePlus 2017 12 月赛」白金元首与独舞

    description 题面 data range \[ 1 \leq T \leq 10, 1 \leq n, m \leq 200 , 0 \leq k \leq \min(nm, 300)\] ...

  8. IP协议简介

    一.IP 1.IP是TCP/IP协议簇中最为核心的协议,所有的TCP.UDP.ICMP及IGMP数据都是以IP数据报格式传输. 2.IP提供不可靠.无连接的数据报传送服务 (1)不可靠:不保证IP数据 ...

  9. [Leetcode] maximun subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  10. POJ.1287 Networking (Prim)

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