HDUOJ 2795 Billboard

本文介绍了一道经典的线段树题目,通过寻找最高效放置板的位置解决实际问题。注意处理边界条件,确保代码逻辑清晰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很经典的线段树题目,只需要每次找尽量靠上并且能放下这块板的位置就行了。

p.s:注意(1 <= h,w <= 10^9),如果h>n,h等于n就行了,因为最多只有n块板。

#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define For(x, y) for(int i=x; i<=y; i++)
#define Lson l,mid,k<<1
#define Rson mid+1,r,k<<1|1
#define Mid(x,y) (x+y)>>1
using namespace std;
const int M=200010;
struct Seb_tree
{
	int val;
};
Seb_tree t[M*4];
int h, w, n;
int s;
void push_up(int k)
{
	t[k].val=max(t[k*2+1].val, t[k*2].val);
}
void build(int l, int r,int k)
{
	t[k].val=w;
	if (l==r) return;
	int mid=Mid(l,r);
	build(Lson);
	build(Rson);
	
}
void query(int x, int l, int r,int k)
{
	if (l==r)
	{
		t[k].val-=x;
		s=l;
		return;
	}
	int mid=Mid(l,r);
	if (x<=t[k*2].val) query(x,Lson);
	else  query(x, Rson);
	push_up(k);
}
int main()
{
	while (scanf("%d %d %d", &h, &w, &n)!=EOF)
	{
		if(h>n) h=n; 
		build(1,h,1);
		For(1,n)
		{
			int x;
			scanf("%d", &x);
			if (x>t[1].val) printf("-1\n");
			else 
			{
			    s=0;
				query(x,1,h,1);
				printf("%d\n", s);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值