【杭电oj】2795 - Billboard(线段树)

来源:互联网 发布:淘宝卖假货怎么投诉 编辑:程序博客网 时间:2024/06/06 04:34

Billboard

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


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
 

Author
hhanger@zju
 

Source
HDOJ 2009 Summer Exercise(5)


题意:

在学校的入口处有一个巨大的矩形广告牌,高为h,宽为w。所有种类的广告都可以贴,比如ACM的广告啊,还有餐厅新出了哪些好吃的,等等。。
 
在9月1号这天,广告牌是空的,之后广告会被一条一条的依次贴上去。
 
每张广告都是高度为1宽度为wi的细长的矩形纸条。
 
贴广告的人总是会优先选择最上面的位置来帖,而且在所有最上面的可能位置中,他会选择最左面的位置,而且不能把已经贴好的广告盖住。

如果没有合适的位置了,那么这张广告就不会被贴了。
 
现在已知广告牌的尺寸和每张广告的尺寸,求每张广告被贴在的行编号。


就是尽量往左上方贴广告。


题解:这道题用线段树,优先往节点的左孩子找(靠上),如果不行再找右孩子。

代码如下:

#include <cstdio>#include <algorithm>using namespace std;#define L o<<1#define R o<<1|1 #define MAX 200000//h给的很吓人,但是如果h大于m,多余的部分就没有意义了 int h,w;int ans;int b[MAX+22];struct TREE{int Max;int o,l,r; }tree[MAX<<2];void PushUp(int o){tree[o].Max = max (tree[L].Max , tree[R].Max);}void Build(int o,int l,int r){tree[o].l = l;tree[o].r = r;tree[o].Max = w;if (l == r)return;int mid = (l + r) >> 1;Build(L,l,mid);Build(R,mid+1,r);}void Query(int o,int x){if (tree[o].l == tree[o].r){tree[o].Max -= x;ans = tree[o].l;return;}if (tree[L].Max >= x)//优先寻找左孩子 Query(L,x);elseQuery(R,x);PushUp(o);}int main(){int m;while (~scanf ("%d %d %d",&h,&w,&m)){if (h > m)h = m;Build(1,1,h);for (int i = 1 ; i <= m ; i++)scanf ("%d",&b[i]);for (int i = 1 ; i <= m ; i++){if (tree[1].Max < b[i])//首先判断所有数的最大值 {printf ("-1\n");continue;}Query (1,b[i]);printf ("%d\n",ans);}}return 0;}


0 0
原创粉丝点击