题目大意:有一个h*w的公告榜,可以依次在上面添加信息。每个信息的长度为x,高为1.

优先在最上面加入,如果空间足够的话,然后优先放在最左面。统计每条公告最终的位置,即它所在的行数。

这里是线段树来存储 当前区间(i,j)的所有位置,剩余的最大空间。 初始化即为w,公告榜的宽。

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
 
#include <iostream>
#include <stdio.h>
using namespace std; int tree[1000000]; // 记录当前区间所有位置,剩余的最大空间
int h,w,n,t; int max(int a,int b){
return a > b ? a : b;
}
void build(int l,int r,int k) {
tree[k] = w;
if (l == r) return ;
int m = (l + r) >> 1;
build(l,m,k*2);
build(m+1,r,k*2+1);
}
void update(int t,int l, int r, int k){
if(t > tree[k]){ // 如果当前无法加入,就直接返回
printf("-1\n");
return;
}
if(l == r){ //说明找到了合适位置可加入。
printf("%d\n", l);
tree[k] -= t;
return;
}
int m = (l+r)/2;
if(t <= tree[k*2])
update(t, l, m, k*2); //优先加入左子树,即上面
else if(t <= tree[k*2+1])
update(t, m+1, r, k*2+1);
tree[k] = max(tree[k*2], tree[2*k+1]);
} int main() {
//freopen("in.txt", "r" ,stdin);
while(scanf("%d %d %d", &h, &w, &n) != EOF){
if(h > n)
h = n;
build(1,h,1);
while(n--){ //更新
scanf("%d", &t);
update(t, 1, h ,1);
}
}
return 0;
}

线段树练习[单点更新] HDU 2795 Billboard的更多相关文章

  1. hdu1754线段树的单点更新区间查询

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

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

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

  3. 线段树(单点更新) HDOJ 2795 Billboard

    题目传送门 /* 主要利用线段树求区间最值,sum[]代表位置可用空间 每次找到最大值的位置 功能:查询最靠前能容纳广告的位置 */ #include <cstdio> #include ...

  4. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  5. [HDOJ2795]Billboard(线段树,单点更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:w*h的公告板要贴公告,公告是w*1的,每个公告有先后顺序,要使每个公告贴的位置尽可能地高 ...

  6. HDU 1394 Minimum Inversion Number(线段树的单点更新)

    点我看题目 题意 :给你一个数列,a1,a2,a3,a4.......an,然后可以求出逆序数,再把a1放到an后,可以得到一个新的逆序数,再把a2放到a1后边,,,,,,,依次下去,输出最小的那个逆 ...

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

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

  8. POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)

    点我看题目 题意 :N个村子连成一条线,相邻的村子都有直接的地道进行相连,不相连的都由地道间接相连,三个命令,D x,表示x村庄被摧毁,R  ,表示最后被摧毁的村庄已经重建了,Q x表示,与x直接或间 ...

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

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

随机推荐

  1. 不改变中间层,如何玩转 .NET 的远程处理功能?

    原文链接: https://msdn.microsoft.com/enus/library/aa289846(v=vs.71).aspx Visual Studio .NET 2003 该方案展示了传 ...

  2. HDU3367+并查集应用

    题意:找到一个这样的图,在这个图中,最多有一个环. 使得所有的边的和最大. 贪心+并查集 首先把边排序,然后开始分类讨论. 对于边ab(含有两个端点ab) 如果a,b是属于两个不同的集合 a b 是两 ...

  3. 如何用AndroidStudio导入github项目

    最近一直在研究AndroidStudio,但是总会有这样那样的问题,特别是在github上看到一个很好地开源项目,想clone下来用用,就会出现很多蛋疼的问题,今天摸索着,结合一些大牛们的建议,轻轻松 ...

  4. 为什么要重写equals()方法与hashCode()方法

    在java中,所有的对象都是继承于Object类.Ojbect类中有两个方法equals.hashCode,这两个方法都是用来比较两个对象是否相等的. 在未重写equals方法我们是继承了object ...

  5. 【HDOJ】3016 Man Down

    线段树+spfa求最长路.逆向思维,从最底下一块板子建图.需要注意的是任何一个板子掉落下面再无板子,此时都可以看做一个终结状态. /* 3016 */ #include <iostream> ...

  6. BZOJ_1616_[Usaco2008_Mar]_Cow_Travelling_游荡的奶牛_(DP)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1616 给出一张图,有些点不能走,给出起始点和结束点,以及时间,求在该时间到达结束点的方案数. ...

  7. zoj 1033 与其说是搜索,不如说是枚举

    zoj 与其说是搜索,不如说是枚举,只不过是通过搜索来实现的罢了. 主要是要注意好闰年的判断,特别是要注意好一串数字的划分. 题意其实我也看了一个晚上,才渐渐的看懂. 题意: 给你一个字符串,其中包含 ...

  8. Android图片上传,可以选择多张图片,缩放预览,拍照上传等

    仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...

  9. strtok

    1. Int main(void) { char *tmp = NULL; Char *remotebuf=”0\r\n”; tmp = strtok(remotebuf, DELIM);      ...

  10. [MarsZ]程序猿谈大学之工作三年半的程序猿给大学童鞋的一些注释

    我本不是个喜欢写文章的人,更甭提写这种基本没技术含量的文章了.但是今天上班的时候,不经意浏览了下学校的QQ群,突然很想把自己的经验经历分享给还在大学里的同学,希望能让某些对前途职业迷茫的童鞋有所帮助. ...