Billboard

来源:互联网 发布:mac winebottler 1.8 编辑:程序博客网 时间:2024/05/17 08:56

线段树的题目,题目已经明确说明广告要尽量贴在靠近顶部同时靠近左边的地方,那就从线段树最左端的树叶开始查找,若找到可以贴广告的位置,就用该节点的记录的广告牌的宽度减去广告的宽度,其父节点记录左右节点中的最大值,若父节点所记录的值都小于广告的宽度,则表明父节点所控制的这个区间里无法贴下这张广告,广告牌实际用到的高度也不会超过广告的数目

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

#include<stdio.h>#include<string.h>#include<algorithm>#define maxn 200005using namespace std;int _max[maxn*4],h,w,n,ans;bool update(int i,int data,int left,int right);void pushup(int i);int main(){while(scanf("%d%d%d",&h,&w,&n)!=EOF){int lim=min(h,n);fill(_max,_max+4*lim,w);for(int i=0;i<n;i++){int a;scanf("%d",&a);ans=-1;update(1,a,1,lim);printf("%d\n",ans);}}return 0;}bool update(int i,int data,int left,int right){if(_max[i]<data)return false;if(left==right){if(_max[i]<data)return false;else{_max[i]-=data;ans=left;return true;}}int mid=(left+right)>>1;if(update(i<<1,data,left,mid)){pushup(i);return true;}if(update(i<<1|1,data,mid+1,right)){pushup(i);return true;}}void pushup(int i){_max[i]=max(_max[i<<1],_max[i<<1|1]);}

#include<stdio.h>#include<string.h>#include<algorithm>#define maxn 200005using namespace std;int h,w,n,row;struct list{int left;int right;int _max;}tree[maxn*4];void pushup(int i){tree[i]._max=max(tree[i<<1]._max,tree[i<<1|1]._max);}void build(int i,int left,int right){tree[i].left=left;tree[i].right=right;tree[i]._max=w;if(left==right)return ;int mid=(left+right)>>1;build(i<<1,left,mid);build(i<<1|1,mid+1,right);}void update(int i,int data,int left,int right){if(row)return ;if(tree[i]._max<data)return ;if(left==right){tree[i]._max-=data;row=left;return ;}int mid=(left+right)>>1;update(i<<1,data,left,mid);update(i<<1|1,data,mid+1,right);pushup(i);}int main(){while(scanf("%d%d%d",&h,&w,&n)!=EOF){int m;memset(tree,0,sizeof(tree));if(h>maxn)h=maxn;build(1,1,h);for(int i=0;i<n;i++){row=0;scanf("%d",&m);update(1,m,1,h);if(!row)printf("-1\n");elseprintf("%d\n",row);}}return 0;}