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;
}
目录
相关文章
|
人工智能 Java
2021杭电多校5-Arrary-hdu7020
Array Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 965 Accepted Submission(s): 312 Problem Description Given an integer array a[1…n].
178 0
2021杭电多校5-Arrary-hdu7020
|
Java 人工智能 Windows
|
机器学习/深度学习 算法
hdu 1892 See you~
点击打开hdu 1892 思路: 二维树状数组 分析: 1 题目给定4种操作:  S x1 y1 x2 y2 询问以(x1 , y1) - (x2 , y2)为对角线的矩形的面积,但是这个对角线不一定是正对角线。
1015 0