码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2795 Billboard(线段树)

时间:2017-07-17 12:31:53      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:puts   placed   ges   ret   mis   file   ota   cst   rect   

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=2795

题目:

Billboard

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

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
 
这道题拓宽我对线段树的应用,原先做的题都是给定一个区间或具体的点去寻找,而这道题是根据树的节点去找。
 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 const int N=2e5+5;
 5 int n,w,h;
 6 int a[N];
 7 struct node{
 8     int l,r;
 9     int MAX;
10 }tree[N*4];//只要开到8e5就可以了,布告栏的高度n以上的结果都一样
11 void build(int l,int r,int i){
12     if(i>4*n)   return ;
13     tree[i].l=l;
14     tree[i].r=r;
15     tree[i].MAX=w;//当区间的MAX小于给定的广告宽时,一定不能张贴,反之则肯定可以
16     if(l==r)    return ;
17     int mid=(l+r)/2;
18     build(l, mid, 2*i);
19     build(mid+1, r, 2*i+1);
20     
21 }
22 void update(int i,int x){
23     if(tree[i].l==tree[i].r){
24         tree[i].MAX-=x;
25         printf("%d\n",tree[i].l);
26         return ;
27     }
28     if(tree[2*i].MAX>=x)    update(2*i, x);
29     else if(tree[2*i+1].MAX>=x) update(2*i+1, x);
30     tree[i].MAX=max(tree[2*i].MAX, tree[2*i+1].MAX);
31 }
32 int main(){
33     while(scanf("%d%d%d",&h,&w,&n)!=EOF){
34         for (int i=0; i<n; i++)     scanf("%d",&a[i]);
35         build(1, min(h,n), 1);//注意树的区间只要开到min(h,n)即可
36         for (int i=0; i<n; i++) {
37             if(tree[1].MAX>=a[i])   update(1, a[i]);
38             else printf("-1\n");
39         }
40     }
41     return 0;
42 }

 

HDU 2795 Billboard(线段树)

标签:puts   placed   ges   ret   mis   file   ota   cst   rect   

原文地址:http://www.cnblogs.com/uniles/p/7193545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!