HDU2795

Posted Exbilar

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU2795相关的知识,希望对你有一定的参考价值。

Billboard

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


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的板子,让你在上面贴小广告,优先往左上角放,且不能相互覆盖,如果能贴上去,则输出他所在的行数,如果不能,就输出-1.
 
  一道奇怪的线段树的题目,刚开始看还以为是二维线段树,后面仔细一想原来还是一维的。我们只需要维护每行中最大能贴宽为多少的小广告就可以了,有了这个想法这道题目就比较好做了。再说一下特判,如果线段树中$[1,n]$中最大能贴的宽度也小于当前小广告的宽度,则输出-1.最后还要有一个小优化,那就是在建树时只需要建到min(h,n)就可以了,因为广告最多也就每行贴一个。还要注意下常数,由于我自带大常数,还超时了几次。
 
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 static const int maxm=1e6+10;
 7 
 8 int tr[maxm],left[maxm],right[maxm];
 9 
10 void build(int num,int l,int r,int w){
11     left[num]=l;right[num]=r;
12     if(l==r){tr[num]=w;return;}
13     int mid=(l+r)>>1;
14     build(num<<1,l,mid,w);
15     build(num<<1|1,mid+1,r,w);
16     tr[num]=max(tr[num<<1],tr[num<<1|1]);
17 }
18 
19 void insert(int num,int width){
20     if(left[num]==right[num]&&tr[num]>=width){printf("%d\\n",left[num]);tr[num]-=width;return;}
21     else{
22         if(width<=tr[num<<1])insert(num<<1,width);
23         else insert(num<<1|1,width);
24         tr[num]=max(tr[num<<1],tr[num<<1|1]);
25     }
26 }
27 
28 int main(){
29     int h,n,w;
30     while(scanf("%d%d%d",&h,&w,&n)!=EOF){
31         build(1,1,min(h,n),w);
32         for(int i=1;i<=n;i++){
33             int x;scanf("%d",&x);
34             if(x>tr[1])printf("-1\\n");
35             else insert(1,x);
36         }
37     }
38     
39     return 0;
40 }
View Code
 

以上是关于HDU2795的主要内容,如果未能解决你的问题,请参考以下文章

hdu 2795 Billboard

hdu 2795 Billboard(线段树)

HDU 2795 Billboard(线段树)

HDU2795

HDU 2795

hdu 2795 Billboard