Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14772 Accepted Submission(s): 6311
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
Sample Output
Author
hhanger@zju
Source
HDOJ 2009 Summer Exercise(5)
题目大意:有一块电子广告牌,高h,宽w,有n条广告,每条广告占用的宽度分别为Wi,占用的高度为1,广告按次序放入广告牌,每次尽量往最高、最左的位置放
求每条广告所放位置在广告牌的第几行
代码贴上:
/**线段树思路:
*将广告牌的高度分成1~h的区间
*树的节点用来存储每段区间中剩下的长度的最大值
*叶节点就表示第L层还剩下的空间长度(L为该节点的左边界)
*每次存入一个Wi,就查询区间中是否还有这么多的空间即可
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 200005
using namespace std;
int Tree[N << 2];
void PushUp(int rt)
{
Tree[rt] = max(Tree[rt << 1],Tree[rt << 1 | 1]);
}
void build(int w,int l,int r,int rt)//初始值为W,最大宽度
{
int mid;
Tree[rt] = w;
if(l == r) return;
mid = (l + r) >> 1;
build(w,l,mid,rt << 1);
build(w,mid + 1,r,rt << 1 | 1);
}
int Query(int p,int l,int r,int rt)//查询操作中包括了UPdate更新操作,p为查询值,rt为当前节点,l,r为控制边界变量
{
int mid,ret;
if(l == r)//找到能放下p的叶节点,更新空间长度
{
Tree[rt] -= p;
return l;//叶节点的区间值就是广告牌的层数
}
mid = (l + r) >> 1;
ret = (Tree[rt << 1] >= p) ? Query(p,l,mid,rt << 1) : Query(p,mid + 1,r,rt << 1 | 1);//如果左子树不能放下p,那么右子树一定可以放下p
PushUp(rt);
return ret;
}
int main()
{
int h,w,n;
int i;
int ans;
int temp;
//freopen("FileIn.txt","r",stdin);
//freopen("FileOut.txt","w",stdout);
while(scanf("%d%d%d",&h,&w,&n) != EOF)
{
if(h > n) h = n;//广告所占用的层数一定不会大于n,因为一共才n条广告
build(w,1,h,1);
for(i = 0; i < n; i++)
{
scanf("%d",&temp);
if(Tree[1] < temp) ans = -1;//如果根节点的值都比temp小,那么一定放不下temp
else ans = Query(temp,1,h,1);
printf("%d\n",ans);
}
}
//fclose(stdin);
//fclose(stdout);
return 0;
}