HDU 2795 Billboard

Thinking: There are 2 tips for this problem. 

(1) If we use struct , TLE may appear. Instead, I changed to use array maxl[] to store the largest space left in a row in this interval. 

(2) Since n <= 200000, we needn't declare an 10^9 array. If the h is larger than n , we make h = n.



AC code:

#include<iostream>
#define maxn 200222
#define MAX(a,b) ((a)>(b)?(a):(b))
using namespace std;

int maxl[maxn * 4];
int h, w, n;
void build(int rt, int left, int right)
{
	maxl[rt] = w;
	if (left == right)
	{
		return;
	}
	int mid = (left + right) >> 1;
	build(rt << 1, left, mid);
	build(rt << 1|1, mid+1, right);
	
}
void push_up(int rt)
{
	maxl[rt] = MAX(maxl[rt << 1], maxl[rt << 1 | 1]);
}
inline int query(int rt, int left,int right,int val)
{
	if (left == right)
	{
		maxl[rt] -= val;
		return left;
	}

	int mid = (left + right) >> 1;
	if (maxl[rt<<1] >= val)
	{
		int ans = query(rt << 1,left,mid,val);
		push_up(rt);
		return ans;
	}
	else
	{
		int ans = query(rt << 1|1,mid+1,right, val);
		push_up(rt);
		return ans;
	}
}

int main()
{

	while (scanf("%d%d%d", &h, &w, &n) != EOF)
	{
		if (h > n)
			h = n;
		build(1, 1, h);
		
		int temp;

		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &temp);
			if (maxl[1] < temp)
				printf("-1\n");
			else printf("%d\n", query(1,1,h,temp));
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值