hdu 2795 Billboard 线段树

来源:互联网 发布:java希尔排序算法 编辑:程序博客网 时间:2024/06/12 21:08

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 5
2
4
3
3
3

Sample Output
1
2
1
3
-1
解题思路:线段树的题也写的有一些了,可做这个题的时候还是没有看出看是要用线段树来解,想用模拟来解,结果开不了那么大的数组,看了别人说要用线段树来解,就想着怎么建树,因为h最大为10^9以他来做线段树根节点的长度,有点不切实际,所以就以min(h,n)为线段树根节点的长度,宽度看作每个节点的值,这样的话所有的叶子节点从左到右就是广告板的高度了,在贴广告时,即更新线段树操作时,每次要优先往线段树左边查找如果左边的关键值比我们要贴的广告长度还要长的话就继续往左找,知道找到叶子结点,先左再右不过都是一直找到叶子节点然后输出节点的左区间就可以了。

#include <bits/stdc++.h>#define N 200005using namespace std;struct SeTree{    int lchild,rchild,key;};int h,w,n;SeTree st[4*N];void Creat(int l,int r,int k){    st[k].lchild = l;    st[k].rchild = r;    if(l == r){        st[k].key = w;        return;    }    int mid = (l+r)/2;    Creat(l,mid,2*k);    Creat(mid+1,r,2*k+1);    st[k].key = max(st[2*k].key,st[2*k+1].key);//这里上浮为左右节点的最大值的原因是,因为我们不确定是否前面的行还可以贴广告,一旦最大值大于等于我们要贴广告的长度时,说明前面还可以贴,所有要优先往前贴,更新操作也是同样}void update(int k,int wi){    if(st[k].lchild == st[k].rchild)    {        st[k].key -= wi;        cout<<st[k].lchild<<endl;        return;    }    if(st[2*k].key>=wi)        update(2*k,wi);    else if(st[2*k+1].key>=wi)        update(2*k+1,wi);    st[k].key = max(st[2*k].key,st[2*k+1].key);}int main(){    int wi;    while(cin>>h>>w>>n)    {        Creat(1,min(h,n),1);//        for(int i=1;i<=2*min(h,n)-1;i++)//            cout<<st[i].lchild<<' '<<st[i].rchild<<' '<<st[i].key<<endl;        for(int i=0;i<n;i++)        {            scanf("%d",&wi);            if(st[1].key<wi)                cout<<"-1"<<endl;            else               update(1, wi);        }    }    return 0;}
原创粉丝点击