您的位置:首页 > 其它

HDU-2795-Billboard

2015-11-02 21:08 246 查看
Billboard

Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 16344 Accepted Submission(s): 6928

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的矩形,有n张1*w的小矩形的公告,问每张公告贴的行数?

这道题需要换一种思维来想,因为公告是1*w的,那么就是说每张公告最多只能占1行x列,x待定。那么我把整张矩阵旋转90度,那么h就会变成列数,而w就会变成行数,由于题目要求是越靠近左边越好的,因此我把每一列当成是线段树的叶子结点就行了,但是这里有个问题就是h最大为10^9, 可是我们不能忽略此时的n值,因为n<=200000,因为每一张最多就一行,所以相当于叶子结点为h和n的最小值,同时我们需要维护区间的最大值,这里的最大值是什么?是这个区间能容纳的最大的长度,那么我们就可以判断这个区间是否可以容纳下本广告了,假如不行的话就返回-1,可以的话还要优先考虑左子树,因为题目要求广告越上越左越好

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int maxn=200100;
struct tnode
{
int left,right;
int len;
int maxlen;
};
struct tnode tree[maxn*4];
int h,w,n;
void buildtree(int left, int right,int node)
{
tree[node].left=left;
tree[node].right=right;
tree[node].len=0;
tree[node].maxlen=0;
if(left==right)
{
tree[node].len=w;
tree[node].maxlen=w;
return ;
}
else
{
int mid=(left+right)/2;
buildtree(left,mid,node*2);
buildtree(mid+1,right,node*2+1);
tree[node].maxlen=max(tree[node*2].maxlen,tree[node*2+1].maxlen);
}
}
int querytree(int len,int node)
{
int clen=0;
if(len>tree[node].maxlen)
{
return -1;
}
else
{
if(tree[node].left==tree[node].right)
{
tree[node].maxlen-=len;
return tree[node].left;
}
else
{
int mid=(tree[node].left+tree[node].right)/2;
if(tree[node*2].maxlen>=len)
{
clen=querytree(len,node*2);
}
else if(tree[node*2+1].maxlen>=len)
{
clen=querytree(len,node*2+1);
}
else
{
clen=-1;
}
}
}
tree[node].maxlen=max(tree[node*2].maxlen,tree[node*2+1].maxlen);
return clen;
}
int main(void)
{
while(scanf("%d %d %d",&h,&w,&n)!=EOF)
{
memset(tree,0,sizeof(tree));
buildtree(1,min(n,h),1);
for(int i=1;i<=n;i++)
{
int c;
scanf("%d",&c);
printf("%d\n",querytree(c,1));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm hdu