首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【杭电oj】2795 - Billboard(线段树)

【杭电oj】2795 - Billboard(线段树)

作者头像
FishWang
发布2025-08-26 18:40:51
发布2025-08-26 18:40:51
6500
代码可运行
举报
运行总次数:0
代码可运行

Billboard

Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17686 Accepted Submission(s): 7450 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

代码语言:javascript
代码运行次数:0
运行
复制
   3 5 5
2
4
3
3
3

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
   1
2
1
3
-1

Author

hhanger@zju

Source

HDOJ 2009 Summer Exercise(5)

题意:

在学校的入口处有一个巨大的矩形广告牌,高为h,宽为w。所有种类的广告都可以贴,比如ACM的广告啊,还有餐厅新出了哪些好吃的,等等。。

在9月1号这天,广告牌是空的,之后广告会被一条一条的依次贴上去。

每张广告都是高度为1宽度为wi的细长的矩形纸条。

贴广告的人总是会优先选择最上面的位置来帖,而且在所有最上面的可能位置中,他会选择最左面的位置,而且不能把已经贴好的广告盖住。

如果没有合适的位置了,那么这张广告就不会被贴了。

现在已知广告牌的尺寸和每张广告的尺寸,求每张广告被贴在的行编号。

就是尽量往左上方贴广告。

题解:这道题用线段树,优先往节点的左孩子找(靠上),如果不行再找右孩子。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <algorithm>
using namespace std;
#define L o<<1
#define R o<<1|1 
#define MAX 200000		//h给的很吓人,但是如果h大于m,多余的部分就没有意义了 
int h,w;
int ans;
int b[MAX+22];
struct TREE
{
	int Max;
	int o,l,r; 
}tree[MAX<<2];
void PushUp(int o)
{
	tree[o].Max = max (tree[L].Max , tree[R].Max);
}
void Build(int o,int l,int r)
{
	tree[o].l = l;
	tree[o].r = r;
	tree[o].Max = w;
	if (l == r)
		return;
	int mid = (l + r) >> 1;
	Build(L,l,mid);
	Build(R,mid+1,r);
}
void Query(int o,int x)
{
	if (tree[o].l == tree[o].r)
	{
		tree[o].Max -= x;
		ans = tree[o].l;
		return;
	}
	if (tree[L].Max >= x)		//优先寻找左孩子 
		Query(L,x);
	else
		Query(R,x);
	PushUp(o);
}
int main()
{
	int m;
	while (~scanf ("%d %d %d",&h,&w,&m))
	{
		if (h > m)
			h = m;
		Build(1,1,h);
		for (int i = 1 ; i <= m ; i++)
			scanf ("%d",&b[i]);
		for (int i = 1 ; i <= m ; i++)
		{
			if (tree[1].Max < b[i])		//首先判断所有数的最大值 
			{
				printf ("-1\n");
				continue;
			}
			Query (1,b[i]);
			printf ("%d\n",ans);
		}
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Billboard
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档