hdoj 2795 Billboard 【线段树】&&【二叉树】

来源:互联网 发布:平面图纸制作软件 编辑:程序博客网 时间:2024/06/14 05:42

Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16309    Accepted Submission(s): 6906


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
3 5 524333
 

Sample Output
1213-1
分析:
线段树题。题意:大学门口有一个巨大的矩形布告板,高度为h,宽度为w。这个广告牌会被贴上许多高度为1宽度未知的公告,为了使公告尽可能的被更多人看到,所以公告会被贴得尽量高并且总是选择最靠左边的位置。假设布告板的高度从高到低编号为1~h,现在有n个公告要贴,告诉你每一个公告的宽度,如果这个公告能被贴在布告板上,输出它的高度编号,否则输出-1。

       我的解题思路:注意到h与w的范围可以达到10的9次方,如果以h的范围来建树应该会爆内存。但是n的最大值不过是20万而已,分析一下可知n个公告最多占用布告板的高度为n。所以应该可以用n的范围来开内存,然后以min(n, h)的值作为区间范围来建树。节点存储高度编号区间的剩余可用宽度最大值就可以了。

代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>#define N 200000#define min(x,y) (x<y?x:y)#define max(x,y) (x>y?x:y)using namespace std; int h,w,q;struct node{int l;int r;int val;}t[N*4];void build(int id,int l,int r){t[id].l=l;t[id].r=r;t[id].val=w;if(l==r)return;int mid=(l+r)>>1;build(id<<1,l,mid);build(id<<1|1,mid+1,r);}int update(int id,int n){if(t[id].l==t[id].r){t[id].val-=n;return t[id].l;}int cnt;if(t[id<<1].val>=n)cnt=update(id<<1,n);elsecnt=update(id<<1|1,n);t[id].val=max(t[id<<1].val,t[id<<1|1].val);return cnt;}int main(){int n,i;while(scanf("%d%d%d",&h,&w,&q)!=EOF){build(1,1,min(h,q));while(q--){scanf("%d",&n);if(t[1].val<n)printf("-1\n");elseprintf("%d\n",update(1,n));}}return 0;}


0 1
原创粉丝点击