您的位置:首页 > 其它

hdu2795-Billboard (线段树求区间最大值)

2017-07-12 14:33 369 查看




Billboard

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

Total Submission(s): 22862    Accepted Submission(s): 9487


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

题目题意:给你一个矩形的广告牌,现在要在这个广告牌上写广告,注意广告不能写在两行,只能写在一行,然后告诉你这个矩形的高度和宽度,高度意味着有多少行,一行占据一个高度,最后在给你要写的广告的数量,每一个广告要给出它能否写上去,如果可以给出它可以写的位置,不能写上去就输出-1.
解题思路: 可以利用线段树的思想,先以每一行的长度即矩形的宽度为叶子节点建立一个树,每一个非叶子节点表示该节点所在区间的剩余的最大长度,建好树之后我们得到的树的根节点表示的是整个矩形的剩余的最大长度,根据这个条件就可以直接得到该条广告是否能够写上去,能写上去的话我们可以直接更新和这个叶子节点有关的所有节点。
ac代码:
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
#define max(a,b) a>b?a:b
int h, w, n;
int sum[200001<<2];
void Build(int l,int r,int rt)
{
sum[rt] = w;//每一个叶子节点在开始的时候的长度就是他的最大长度,即矩形的宽度
if(l == r) return ;
int m = (l + r) >> 1;
Build(l, m, rt<<1);//递归建树
Build(m+1, r, rt<<1|1);
}
int  quest(int l,int r,int t,int rt)
{
if(l == r) {
sum[rt] -= t;//在查询到叶子节点的时候更新
return l; // 返回所在的叶子的位置,即矩形的行位置
}
int m = (l + r) >> 1;
int ret ;
if(sum[rt<<1] >= t) ret = quest(l, m, t, rt<<1);
else ret = quest(m+1, r, t, rt<<1|1);
sum[rt] = max(sum[rt<<1],sum[rt<<1|1]); //回溯更新节点
return ret;
}
int main()
{
int t;
while(~scanf("%d%d%d", &h, &w, &n))
{
if(h > n)  h = n;//很重要 (因为可以直接一行一个广告,可以节省树的规模)
Build(1,h,1);//建树
while(n--)
{
scanf("%d",&t);
if(sum[1]<t) printf("-1\n");
else printf("%d\n",quest(1,h,t,1));
}
}
return 0;
}

题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=2795
有关线段树的更多知识可以看点击打开链接http://blog.csdn.net/wang_heng199/article/details/74938672
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: