hdu 2795 公告板 (单点最值)

题意:有个公告板,大小为h*w,要贴n张公告,每个公告的长度是x,高度固定为1,公告放的要尽可能靠上并尽可能靠左,每给出一张公告,要求这个公告在满足要求的情况下放在了第几层。

Sample Input
3 5 5
2
4
3
3
3

Sample Output
1
2
1
3
-1

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # include <queue>
 7 # define LL long long
 8 using namespace std ;
 9
10 const int maxn = 200010;
11
12 int MAX[maxn<<2] ; //结点开4倍
13 int h , w , n ;
14
15 void PushUP(int rt) //更新到父节点
16 {
17     MAX[rt] = max(MAX[rt * 2] , MAX[rt * 2 + 1] ); //rt 为当前结点
18 }
19
20 void build(int l , int r , int rt) //构建线段树
21 {
22     MAX[rt] = w ;
23     if (l == r)
24     {
25         return ;
26     }
27     int m = (l + r) / 2 ;
28     build(l , m , rt * 2) ;
29     build(m + 1 , r , rt * 2 +1) ;
30
31 }
32
33 int query(int x , int l , int r , int rt)  //区间求最大值的位子 直接把update的操作在query里做了
34 {
35     if (l == r)
36     {
37        MAX[rt] -= x ;
38        return l ;    //每个叶子节点代表公告板的行
39     }
40     int m = (l + r) / 2 ;
41     int ret = 0 ;
42     if (MAX[rt * 2] >= x)
43         ret = query(x , l , m , rt * 2) ;
44     else
45         ret = query(x , m+1 , r , rt * 2 + 1) ;
46     PushUP(rt) ;
47     return ret ;
48 }
49
50 int main ()
51 {
52     //freopen("in.txt","r",stdin) ;
53     while(scanf("%d %d %d" , &h , &w , &n) != EOF)  // h为高 w为宽  n为数量
54     {
55         if (h > n)
56         h = n ;
57         build(1 , h , 1);
58         while (n--)
59         {
60            int x ;
61            scanf("%d" , &x) ;
62            if (x > MAX[1])  //如果一个公告的宽度 比1-h这区间 所有的剩余宽度都大  就是放不下了
63               printf("-1\n") ;
64            else
65               printf("%d\n" , query(x , 1 , h , 1)) ;
66         }
67     }
68
69     return 0 ;
70 }

时间: 2024-08-01 23:31:36

hdu 2795 公告板 (单点最值)的相关文章

HDU 2795 Billboard (线段树单点更新)

题意:h,w,n:有一个h*w尺寸的木板,n张1*wi的海报,贴海报的位置尽量高,尽量往左,问每张海报贴的高度 看到1 <= h,w <= 10^9; 1 <= n <= 200,000,应该就是线段树了. 关键在怎么建树,这里我们对h进行分割,每个高度都有等长的w,我们从上往下贴,如果当前高度 (在同一高度上l==r)的长度可以满足wi则可以贴,否则继续往下寻找. #include <iostream> #include <stdio.h> #includ

HDU 2795——Billboard——————【单点更新、求最小位置】

Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2795 Appoint description:  System Crawler  (2015-04-10) Description At the entrance to the university, there is a huge rectangular billb

HDU 2795 Billboard(线段树,单点更新)

D - Billboard Crawling in process... Crawling failed Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2795 Appoint description: bupt_admin_13 (2015-07-28) System Crawler (2015-08-18) Description

hdu3966 树链剖分(区间更新和单点求值)

http://acm.hdu.edu.cn/showproblem.php?pid=3966 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the ene

HDU 2795 线段树(转变思维方能改变世界)

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9895    Accepted Submission(s): 4413 Problem Description At the entrance to the university, there is a huge rectangular billboard of si

hdu 2795 Billboard(线段树)

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10890    Accepted Submission(s): 4827 Problem Description At the entrance to the university, there is a huge rectangular billboard of

【线段树四】HDU 2795 Billboard

BillboardTime Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9045    Accepted Submission(s): 4021 Problem Description At the entrance to the university, there is a huge rectangular billboard of siz

HDU 2795 Billboard(宣传栏贴公告,线段树应用)

HDU 2795 Billboard(宣传栏贴公告,线段树应用) ACM 题目地址:HDU 2795 Billboard 题意: 要在h*w宣传栏上贴公告,每条公告的高度都是为1的,而且每条公告都要尽量贴最上面最靠左边的,给你一系列的公告的长度,问它们能不能贴上. 分析: 不是很好想,不过想到了就很好写了. 只要把宣传栏倒过来就好办了,这时候就是变成有h条位置可以填公告,填放公告时就可以尽量找最左边的合适的位置来放了. 可以用线段树实现,查找的复杂度是O(logn),需要注意的坑点是h的范围非常

hdu 2795 线段树--点更新

http://acm.hdu.edu.cn/showproblem.php?pid=2795 多校的第一场和第三场都出现了线段树,比赛期间没做,,这两天先做几道热身下,然后31号之前把那两道多校的线段树都搞了,这是一道热身题 关键是建模: 首先一定看清楚题目构造的场景,看有什么特点--------会发现,如果尽量往左上放置的话,那么由于 the i-th announcement is a rectangle of size 1 * wi.,完全可以对h建立线段树,表示一行,结点里的l,r就表示