hdu 2795 Billboard

简介:

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;
}
目录
相关文章
|
6月前
|
Java
HDU-1896-Stones
HDU-1896-Stones
26 0
|
12月前
|
Java
hdu 2503 a/b + c/d
hdu 2503 a/b + c/d
43 0
|
12月前
|
Java
hdu 1257 最少拦截系统
hdu 1257 最少拦截系统
46 0
|
C++
HDU1862
中文题,题意挺好理解,不过多赘述。
1266 0
|
机器学习/深度学习
hdu 2604 Queuing
点击打开hdu 2604 思路: 递推+矩阵快速幂 分析; 1 根据题目的意思,我们可以求出F[0] = 0 , F[1] = 2 , F[2] = 4 , F[3] = 6 , F[4] = 9 , F[5] = 15 2 那么根据上面...
799 0
|
人工智能 BI Java
HDU 1003
Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 105228    Accepted Submission(s): 242...
890 0