hdu 2795 Billboard -- 线段树

该博客介绍了如何运用线段树数据结构解决HDU 2795 Billboard的题目。题目要求在一块h*w的板上,按照特定规则张贴n张1*w_i的海报,并输出每张海报的高度位置。线段树的思路是初始化每个节点的最大值为w,从左端开始寻找合适的张贴位置。
摘要由CSDN通过智能技术生成

hdu 2795 Billboard
题意:
有一块板,规格为 h ∗ w h*w hw,然后有n张海报,每张海报的规格为 1 ∗ w i 1*wi 1wi,选择贴海报的位置是:尽量高,同一高度,选择尽量靠左的地方。要求输出每张海报的高度位置
思路:
以h建立线段树维护最大值每个节点初始化最大值为 w w w
对于每次改,从最左端开始找满足要求的位置

//#include<bits/stdc++.h>
#include<iostream>
#include<vector>
//#include <unordered_map>
using namespace std;
//template<class...Args>
//void debug(Args... args) {//Parameter pack
//    auto tmp = { (cout << args << ' ', 0)... };
//    cout << "\n";
//}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>pll;
typedef pair<int, int>pii;
const ll N = 5e5 + 5;
const ll INF = 0x7fffffff;
const ll MOD = 1e9+7;

int h, w;
struct node {
    int l, r;
    int maxv;
}tree[N<<2];
void push_up(int root) {
    tree[root].maxv = max(tree[root << 1].maxv , tree[root << 1 | 1].maxv);
}
void build(int root, int l, int r) {
    if (l == r)tree[root] = { l,r,w };
    else {
        tree[root] = { l,r };
        int mid = l + r >> 1;
        build(root << 1, l, mid);
        build(root << 1 | 1, mid + 1, r);
        push_up(root);
    }
}
int update(int root,int k) {
    if (tree[root].l == tree[root].r) {
        tree[root].maxv -= k;
        return tree[root].l;
    }
    int t;
    if (tree[root << 1].maxv >= k) t = update(root << 1, k);
    else t = update(root << 1 | 1, k);
    push_up(root);
    return t;
}
int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int n;
    while (cin >> h >> w >> n) {
        build(1, 1, min(200000, h));
        while (n--) {
            int ww;
            cin >> ww;
            if (tree[1].maxv >= ww)cout << update(1, ww) << "\n";
            else cout << -1 << "\n";
        }
    }
    
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值