Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13382 Accepted Submission(s): 5770
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
题目意思:
一个h*w的木板,要在在上面贴n张报纸,每个报纸都是1*w[i]矩形,优先贴在木板的上方和左方,报纸之间不能重叠,若能贴输出所贴位置的h(从上到下1-h),否则输出-1。
思路:
暴力肯定超时,需要转换一下思维,若把木板旋转90度呢?问题就变成:给一h*w的矩形,优先往最左边贴报纸,其次往最左面的下边贴报纸。很明显是线段树嘛,维护横着每个单位最小的高度即可。说着不是太好说,看代码吧,代码是最好的解题报告。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 11 #define N 200005 12 #define ll root<<1 13 #define rr root<<1|1 14 #define mid (a[root].l+a[root].r)/2 15 16 17 int max(int x,int y){return x>y?x:y;} 18 int min(int x,int y){return x<y?x:y;} 19 int abs(int x,int y){return x<0?-x:x;} 20 21 int n; 22 int b[N]; 23 int h, w; 24 int ans[N]; 25 26 struct node{ 27 int l, r, minw; 28 }a[N*4]; 29 30 void build(int l,int r,int root){ 31 a[root].l=l; 32 a[root].r=r; 33 a[root].minw=0; 34 if(l==r) return; 35 build(l,mid,ll); 36 build(mid+1,r,rr); 37 } 38 int flag; 39 40 void solve(int id,int root){ 41 if(b[id]+a[root].minw>w) return; //若这个区间最小的高度+要贴报纸的高度大于木板的高度则不能贴 42 if(a[root].l==a[root].r){ 43 ans[id]=a[root].l; 44 a[root].minw+=b[id]; 45 flag=1; 46 return ; 47 } 48 if(b[id]+a[ll].minw<=w) solve(id,ll); //优先贴最左边 49 else solve(id,rr); 50 a[root].minw=min(a[ll].minw,a[rr].minw);//向上更新 51 } 52 53 main() 54 { 55 int i, j, k; 56 while(scanf("%d %d %d",&h,&w,&n)==3){ 57 for(i=1;i<=n;i++) scanf("%d",&b[i]); 58 build(1,min(n,h),1); 59 for(i=1;i<=n;i++){ 60 if(b[i]>w){ //若b[i]>w,显然不能贴 61 ans[i]=-1;continue; 62 } 63 flag=0; 64 solve(i,1); 65 if(!flag) ans[i]=-1; //若不能贴 66 } 67 for(i=1;i<=n;i++) printf("%d\n",ans[i]); 68 } 69 }