Billboard
Time Limit : 20000/8000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
HDOJ 2009 Summer Exercise(5)
最近在为了蓝桥杯练习线段树,开始看这道题的时候,首先判断了h = 1e9。不能开宽度数组。如果开宽度线段树的话,就是按照高度来计算。之后查阅大佬的题解,原来可以按照高度来判断。之后,由于学习的时候只学了和的线段树,突然傻眼,之后在思考思考,才发现就是最大值的线段树,看来线段树的种类还真是多呢。对于pushUp的规划是线段树的难点之一。。
不过这道题还是很模板的,没有很多节点共同增加,矩形都是高度为1的,所以还是挺好写的。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int maxn = 2e5 + 5;
int w,h,n;
inline int Max(int a ,int b){
return a>b?a:b;
}
inline int Min(int a ,int b){
return a<b?a:b;
}
int sum[maxn<<2];
int add[maxn<<2];
void pushUp(int rt){
sum[rt] = Max(sum[rt<<1] , sum[rt<<1|1]);
}
void build(int l,int r,int rt){
if(l == r){
sum[rt] = w;
return;
}
int m = (l + r) >> 1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
pushUp(rt);
}
int update(int K,int C,int l,int r,int rt){
int ans = 0;
if(K > sum[rt])
return 0;
if(l == r){
sum[rt] -= C;
return l;
}
pushUp(rt);
int m = (l+r)>>1;
if(sum[rt<<1] >= K) ans += update(K,C,l,m,rt<<1);
else ans += update(K,C,m+1,r,rt<<1|1);
pushUp(rt);
return ans;
}
int main()
{
//std::ios::sync_with_stdio(false);
//freopen("test.txt","r",stdin);
while(cin>>h>>w>>n){
int q = Min(h,n);
build(1,q,1);
for(int i=0;i<n;i++){
int x;
scanf("%d",&x);
if(x > sum[1]){
cout<<-1<<endl;
}else{
cout<<update(x,x,1,q,1)<<endl;
}
//cout<<sum[1]<<endl;
}
}
return 0;
}