您的位置:首页 > 其它

HDU--2795--Billboard--线段树

2013-10-30 01:24 281 查看
关于举办计算机学院大学生程序设计竞赛(2013’11)的报名通知 

Billboard

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

Total Submission(s): 8156    Accepted Submission(s): 3629


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 X w的表格,然后是n次输入,每次输入都是一个1 X  Wi的表格,然后插入,你要输出的是这个能不能插下,能的话就输出是第几行,不能就是-1,插入过了的地方不能再用,下面掩饰例1       0表示空的,1表示装了的

0 0 0 0 0    左边是3 X 5的表格,                1 1 0 0 0                     1 1 0 0 0                 1 1 1 1 1

0 0 0 0 0    然后一个一个            输入2       0 0 0 0 0      输入4    1 1 1 1 0    输入3   1 1 1 1 0     这下懂了没?

0 0 0 0 0    输入2,4,3,3,3                  0 0 0 0 0                    0 0 0 0 0                  0 0 0 0 0

#include <iostream>
#include <cstdio>
using namespace std;
int n,m,x,X;
struct node
{
int l,r,m;
}s[1000000];
int Max(int a,int b)
{
return a>b?a:b;
}
void setit(int l,int r,int step)
{
s[step].l=l;
s[step].r=r;
s[step].m=m;
if(l==r)return;
setit(l,(l+r)/2,2*step);
setit((l+r)/2+1,r,2*step+1);
}
void insert(int l,int r,int step)
{
if(s[step].m<x)return;  //如果没有放得下的就只好输出-1得了
s[step].m-=x;
if(l==r){X=l;return;}  //到最底层了就叫做放下来了,记录位置
if(s[2*step].m>=x)insert(l,(l+r)/2,2*step);  //如果左子树能放下
else insert((l+r)/2+1,r,2*step+1);  //不然就放右子树
s[step].m=Max(s[2*step].m,s[2*step+1].m);  //然后更新当前节点的最大容量
}
int main (void)
{
int l;
while(scanf("%d%d%d",&n,&m,&l)!=EOF)
{
n=n<l?n:l;
setit(1,n,1);
while(l--&&scanf("%d",&x))
{
X=-1,insert(1,n,1);printf("%d\n",X);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: