码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2795 Billboard

时间:2015-08-08 13:38:45      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:线段树

hdu 2795 的传送门–>

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*w,要贴n张公告,每个公告的长度是k,高度固定为1,公告放的要尽可能靠上并尽可能靠左,每给出一张公告,要求这个公告在满足要求的情况下放在了第几层。
解题思路: 线段树,详情看代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

const int maxn = 200000+5;

struct
{
    int l, r, num;
} tree[maxn*4];

int w;

void build(int root, int l, int r)
{
    tree[root].l = l;
    tree[root].r = r;
    tree[root].num=w;
    if(l == r)
        return;
    int mid = (l + r) >> 1;
    build(root<<1, l, mid);
    build(root<<1|1 , mid+1, r);
}

void query(int root, int pos)
{
    int l = tree[root].l;
    int r = tree[root].r;
    if(l == r)
    {
        tree[root].num -= pos;
        printf("%d\n",l);
        return;
    }
    if(tree[root<<1].num >= pos)
        query(root<<1, pos);
    else if(tree[root<<1|1].num >= pos)
        query(root<<1|1, pos);
    else
        puts("-1");
    tree[root].num = max(tree[root<<1].num, tree[root<<1|1].num);
}
int main()
{
    int h,n;
    while(cin>>h>>w>>n)
    {
        int x;
        build(1, 1, min(h, maxn));
        for(int i=0; i<n; i++)
        {
            scanf("%d",&x);
            query(1, x);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 2795 Billboard

标签:线段树

原文地址:http://blog.csdn.net/qingshui23/article/details/47357059

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!