Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24251 Accepted Submission(s): 9986
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.
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.
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
3 5 5 2 4 3 3 3
Sample Output
1 2 1 3 -1
Author
hhanger@zju
Source
Recommend
lcy
唔这个算一个线段树的小变形吧。
给你一个长为w,高为h的广告牌,然后给你n个高度为1,宽度为v_i的小广告,让你往广告牌上贴,要求每次贴的时候,尽可能的往顶端贴,当在同一水平高度有多个位置的时候,尽可能的往左边贴。要求输出每次贴的水平高度的行号(1~n,从顶端开始),如果没有可行位置,输出-1。
看起来这题数据很大,w和h都是1e9,但我们发现他最多就给你200,000个小广告,所以我们的广告牌实际上是min(h, n) * w的尺寸。
然后我们考虑把广告牌横过来,这样可以把每次贴广告的步骤当作一次点加操作,不过和之前的定点加不一样。这道题相当于区间内每个点有一个容纳范围(0 ~ w),对于每次操作,让我们返回一个编号最小的点,使得该点能容纳下v_i,然后更新区间(用线段树)。
#include <bits/stdc++.h>
using namespace std;
struct Tree
{
int l;
int r;
int maxx;//表示该区间所能容纳的最大广告长度
};
Tree tree[800010];
int h, w, n;
void build(int x, int l, int r)
{
tree[x].l = l;
tree[x].r = r;
tree[x].maxx = w;//这里直接把初始情况设成了最大值,设成0也可以,不过后面的符号要改
if(l != r){
int mid = l + ((r - l) >> 1);
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
}
}
void up(int x)
{
tree[x].maxx = max(tree[x << 1].maxx, tree[x << 1 | 1].maxx);
}
int update(int x, int v)
{
int ans = -1;
if(tree[x].maxx >= v){
int l = tree[x].l;
int r = tree[x].r;
int mid = l + ((r - l) >> 1);
if(l == r){//更新到点了
tree[x].maxx -= v;
ans = l;//返回点编号
return ans;
}
else if(tree[x << 1].maxx >= v){//优先考虑左子节点
ans = update(x << 1, v);
}
else{
ans = update(x << 1 | 1, v);
}
up(x);
}
return ans;
}
int main(int argc, char const *argv[])
{
while(scanf("%d%d%d", &h, &w, &n) == 3){
build(1, 1, min(h, n));
for(int i = 0; i < n; i ++){
int v;
int ans;
scanf("%d", &v);
ans = update(1, v);
printf("%d\n", ans);
}
}
return 0;
}