题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795
题意就是给一个h*w的矩形,往上面放n个高度为1宽度任意的条,优先选择放在左上方,对于每一个条,问放在了第几行。
题目中h给的范围是10^9,但是实际上不需要那么大,n是多少,h最大也就是多少,最多的情况就是一行贴一条,所以h的范围实际上是200000,这样显然就没有那么虐心了。。每一个结点存储的信息就是再该段区间内所有行中所剩的最大的空间(我自己都说不明白了。。),这样一来,对于每一个条询问的时候,首先能知道该区间段内是否能够放得下这个条,如果能放下就往下找,优先找左子树,左子树放不下再找右子树,一直往下递归,最后放下了以后再更新到上面去。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 200010;
int h, w, n;
int MAX[maxn << 2];
void PushUp(int rt)
{
MAX[rt] = max(MAX[rt << 1], MAX[rt << 1 | 1]);
}
void build(int l, int r, int rt)
{
MAX[rt] = w;
if (l == r) return;
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
}
int query(int x, int l, int r, int rt)
{
if (l == r)
{
MAX[rt] -= x;
return l;
}
int m = (l + r) >> 1;
int ret = (MAX[rt << 1] >= x) ? query(x, l, m, rt << 1) : query(x, m + 1, r, rt << 1 | 1);
PushUp(rt);
return ret;
}
int main()
{
while (scanf("%d%d%d", &h, &w, &n) != EOF)
{
if (h > n) h = n;
build(1, h, 1);
while (n--)
{
int x;
scanf("%d", &x);
if (MAX[1] < x) printf("-1\n");
else printf("%d\n", query(x, 1, h, 1));
}
}
return 0;
}