zoukankan      html  css  js  c++  java
  • hdu 2795 Billboard(线段树)

    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=2795

    题意:

    一个h*w的公告牌,要在其上贴公告。
    输入的是1*wi的w值,这些是公告的尺寸
    接下来要满足的条件: 尽量往上,同一高度尽量靠左。
    求第n个广告所在的行数 ,没有合适的位置贴了则输出-1。

    题解:

    利用线段树可以得出区间的最大值,维护一个最大值val,用来表示这段区间内有最多空位的那一行的空位长度。与输入进来的长度进行比较,先左边比较,再右边。(也就是左子树的最大值大于他,就查询左子树,否则查询右子树)。
    因为n最大为20W啊,只要开min(h,n)即可了。因为最多也就用n行。
    线段树最后一层存的是每行的剩余量,然后父节点存当前子树中最大连续空余。然后类似二分查找。

    代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 #define MS(a) memset(a,0,sizeof(a))
     5 #define MP make_pair
     6 #define PB push_back
     7 const int INF = 0x3f3f3f3f;
     8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
     9 inline ll read(){
    10     ll x=0,f=1;char ch=getchar();
    11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    12     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    13     return x*f;
    14 }
    15 //////////////////////////////////////////////////////////////////////////
    16 const int maxn = 2e5+10;
    17 
    18 struct Node{
    19     int l,r,val;
    20 }node[maxn<<2];
    21 
    22 int h,w,n;
    23 
    24 void pushup(int rt){
    25     node[rt].val = max(node[rt<<1].val,node[rt<<1|1].val);
    26 }
    27 
    28 void build(int l,int r,int rt){
    29     node[rt].l = l;
    30     node[rt].r = r;
    31 
    32     if(node[rt].l == node[rt].r){
    33         node[rt].val = w;
    34         return ;
    35     }
    36     int mid = (l+r)/2;
    37     build(l,mid,rt<<1);
    38     build(mid+1,r,rt<<1|1);
    39     pushup(rt);
    40 }
    41 
    42 int update(int rt,int val){
    43     if(node[rt].val < val) return -1;
    44     if(node[rt].l == node[rt].r && node[rt].val >= val){
    45         node[rt].val -= val;
    46         return node[rt].r;
    47     }
    48 
    49     int ans=-1;
    50     if(val <= node[rt<<1].val)
    51         ans = update(rt<<1,val);
    52     else
    53         ans = update(rt<<1|1,val);
    54     pushup(rt);
    55     return ans;
    56 }
    57 
    58 int main(){
    59     while(scanf("%d%d",&h,&w)!=EOF){
    60         n = read();
    61         build(1,min(h,n),1);
    62         for(int i=0; i<n; i++){
    63             int wi = read();
    64             cout << update(1,wi) << endl;
    65         }
    66     }
    67 
    68     return 0;
    69 }
  • 相关阅读:
    leetcode701. Insert into a Binary Search Tree
    leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
    leetcode 110. Balanced Binary Tree
    leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree
    二叉树
    leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
    5. Longest Palindromic Substring
    128. Longest Consecutive Sequence
    Mac OS下Android Studio的Java not found问题,androidfound
    安卓 AsyncHttpClient
  • 原文地址:https://www.cnblogs.com/yxg123123/p/6827665.html
Copyright © 2011-2022 走看看