Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13382    Accepted Submission(s): 5770

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
 
Author
hhanger@zju
 
Source
 
 
题目意思:
一个h*w的木板,要在在上面贴n张报纸,每个报纸都是1*w[i]矩形,优先贴在木板的上方和左方,报纸之间不能重叠,若能贴输出所贴位置的h(从上到下1-h),否则输出-1。
 
思路:
暴力肯定超时,需要转换一下思维,若把木板旋转90度呢?问题就变成:给一h*w的矩形,优先往最左边贴报纸,其次往最左面的下边贴报纸。很明显是线段树嘛,维护横着每个单位最小的高度即可。说着不是太好说,看代码吧,代码是最好的解题报告。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 200005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n;
int b[N];
int h, w;
int ans[N]; struct node{
int l, r, minw;
}a[N*]; void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].minw=;
if(l==r) return;
build(l,mid,ll);
build(mid+,r,rr);
}
int flag; void solve(int id,int root){
if(b[id]+a[root].minw>w) return; //若这个区间最小的高度+要贴报纸的高度大于木板的高度则不能贴
if(a[root].l==a[root].r){
ans[id]=a[root].l;
a[root].minw+=b[id];
flag=;
return ;
}
if(b[id]+a[ll].minw<=w) solve(id,ll); //优先贴最左边
else solve(id,rr);
a[root].minw=min(a[ll].minw,a[rr].minw);//向上更新
} main()
{
int i, j, k;
while(scanf("%d %d %d",&h,&w,&n)==){
for(i=;i<=n;i++) scanf("%d",&b[i]);
build(,min(n,h),);
for(i=;i<=n;i++){
if(b[i]>w){ //若b[i]>w,显然不能贴
ans[i]=-;continue;
}
flag=;
solve(i,);
if(!flag) ans[i]=-; //若不能贴
}
for(i=;i<=n;i++) printf("%d\n",ans[i]);
}
}

HDU 2795 单点更新,区间优先查找(想法)的更多相关文章

  1. hdu 1166 (单点更新+区间求和+裸题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...

  2. HDU 1754 I Hate It(线段树之单点更新 区间最值查询)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  4. HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)

    HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...

  5. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. LightOJ 1112 Curious Robin Hood (单点更新+区间求和)

    http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i        将第i个数值输出,并将第i个值清0 2 i v     ...

  7. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  8. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  9. HDU 3308 LCIS(线段树单点更新区间合并)

    LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...

随机推荐

  1. [转载] Codis作者黄东旭细说分布式Redis架构设计和踩过的那些坑们

    原文: http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=208733458&idx=1&sn=691bfde670fb ...

  2. 08 Transactions

    本章提要------------------------------------------事务的特性事务控制语句------------------------------------------事 ...

  3. 展讯DTS路径及编译

    DTS路径:/kernel/arch/arm/boot/dts 如何查找修改当前TP的DTS配置(分辨率)的文件:1.查找make file,找关键字都包含CONFIG_MACH,在/kernel/a ...

  4. 使用myeclipse建立maven项目(重要)

    maven是管理项目的,myeclipse是编写代码的.第一次写项目都要配置好多东西,很麻烦,now 来看看怎样新建一个maven项目. 工具/原料   myeclipse maven 方法/步骤   ...

  5. JavaSE 复习_4 接口多态和内部类

    △abstract不可以修饰成员变量 △一个类可以没有抽象方法,可以定义为抽象类,这样的目的是不能让其他类建立本类对象,交给子类完成. △abstract和static(可以被类名调用方法,但是抽象方 ...

  6. Jquery的普通事件和on的委托事件小案例

    以click的事件为例: 普通的绑定事件:$('.btn').click(function(){})绑定 on绑定事件:$(document).on('click','.btn2',function( ...

  7. Javascript模块化编程(一):模块的写法 (转载 学习中。。。。)

    转载地址:http://www.ruanyifeng.com/blog/2012/10/javascript_module.html 阮一峰 大神:http://www.ruanyifeng.com/ ...

  8. velocity基础教程--1.标准使用(zhuan)

    http://llying.iteye.com/blog/387253 **************************** velocity是一个非常好用的模板引擎 这里不对项目进行详细介绍,可 ...

  9. iOS开发 判断字符串是不是表情

    + (BOOL)stringContainsEmoji:(NSString *)string { __block BOOL returnValue = NO; [string enumerateSub ...

  10. drupal_get_path_alias

    drupal_get_path_alias('path','language')这个函数是去读取url_alias表,获得某个path在特定language下的alias列的值--刚开始我 dpm(d ...