• HDU 2795 线段树区间最大值,单点更新+二分


    Billboard

    Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 18496    Accepted Submission(s): 7751


    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
     
    Author
    hhanger@zju
     
    Source
     
    题意:一块h*w的宣传栏 张贴1*wi的海报 海报优先靠上靠左 
            按照次序张贴n张海报 判断海报能够贴在第几行 如果不能张贴则输出‘-1’
     
    题解:线段树处理 存储区间最大的可用的宽度value     二分行数  能够张贴则更新线段树
            注意取h=min(h,n) 
     
      1 //code by  drizzle
      2 #include<iostream>
      3 #include<cstring>
      4 #include<cstdio>
      5 #include<algorithm>
      6 #include<vector>
      7 #define ll __int64
      8 #define PI acos(-1.0)
      9 #define mod 1000000007
     10 using namespace std;
     11 struct node
     12 {
     13     int l;
     14     int r;
     15     int value;
     16 }tree[4*200005];
     17 int h,w,n;
     18 void buildtree(int root,int left,int right)
     19 {
     20     tree[root].l=left;
     21     tree[root].r=right;
     22     tree[root].value=w;
     23     if(left==right)
     24         return ;
     25     int mid=(left+right)>>1;
     26     buildtree(root<<1,left,mid);
     27     buildtree(root<<1|1,mid+1,right);
     28     tree[root].value=max(tree[root<<1].value,tree[root<<1|1].value);
     29 }
     30 void updata(int c,int left,int right,int root)
     31 {
     32     if(tree[root].l==left&&tree[root].r==right)
     33     {
     34         tree[root].value+=c;
     35         return ;
     36     }
     37     int mid=(tree[root].l+tree[root].r)>>1;
     38     if(right<=mid)
     39         updata(c,left,right,root<<1);
     40     else
     41     {
     42         if(left>mid)
     43             updata(c,left,right,root<<1|1);
     44         else
     45         {
     46             updata(c,left,mid,root<<1);
     47             updata(c,mid+1,right,root<<1|1);
     48         }
     49     }
     50     tree[root].value=max(tree[root<<1].value,tree[root<<1|1].value);
     51 }
     52 int query(int root,int left,int right)
     53 {
     54     if(tree[root].l==left&&tree[root].r==right)
     55     {
     56         return tree[root].value;
     57     }
     58     int mid=(tree[root].l+tree[root].r)>>1;
     59     if(right<=mid)
     60      return query(root<<1,left,right);
     61     else
     62     {
     63         if(left>mid)
     64             return query(root<<1|1,left,right);
     65         else
     66             return query(root<<1,left,mid)+query(root<<1|1,mid,right);
     67     }
     68 }
     69 int exm;
     70 int main()
     71 {
     72     while(scanf("%d %d %d",&h,&w,&n)!=EOF)
     73     {
     74         if(h>n)
     75             h=n;;
     76         buildtree(1,1,h);
     77         for(int i=1;i<=n;i++)
     78         {
     79             scanf("%d",&exm);
     80             if(query(1,1,h)<exm)
     81             {
     82                 printf("-1
    ");
     83             }
     84             else
     85             {
     86                 int L=1,R=h,mid;
     87                 while(L<R)
     88                 {
     89                     mid=(L+R)>>1;
     90                     if(query(1,L,mid)>=exm)
     91                         R=mid;
     92                     else
     93                         L=mid+1;
     94                 }
     95                 printf("%d
    ",L);
     96                 updata(-exm,L,L,1);
     97             }
     98         }
     99     }
    100     return 0;
    101 }
  • 相关阅读:
    文件的权限
    Linux正则表达式
    Linux中硬链接与软链接
    Linux下文件属性介绍
    JavaScript核心之语法
    JavaScript概述
    浏览器播放wav语音文件,tomcat异常,ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。
    JS 数据结构-Set 集合 创建Set 常用Set方法
    JSON.parse 方法解析纯数字键值对报错的解决方法
    前端常用框架
  • 原文地址:https://www.cnblogs.com/hsd-/p/5691968.html
Copyright © 2020-2023  润新知