HDU 2795 Billboard(线段树的另类应用)

Billboard

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

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
 
Author
hhanger@zju
 
Source
 

题目链接:HDU 2795

有点意思的一道题目,主要思想就是不停的找容得下所给宽度wid的区间,且要优先找靠左的区间,这个好办,优先递归左子树即可,然后怎么看是否还有空间能放海报呢?那看T[1]即树根,若T[1].max小于wid那说明整个区间最大空闲值都不够,不能放,否则就用update去递归寻找就好了,既然T[1].mx>=wid,那肯定存在某一个点的最大值大于wid,那直接利用update的递归方式不停地无脑二分下去肯定能找到,当然要优先递归左子树。

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=200010;
struct seg
{
int l,mid,r;
int mx;
}T[N<<2];
int h,w,n;
inline int max(int a,int b)
{
return a>b?a:b;
}
void pushup(int k)
{
T[k].mx=max(T[LC(k)].mx,T[RC(k)].mx);
}
void build(int k,int l,int r)
{
T[k].l=l;
T[k].r=r;
T[k].mid=MID(l,r);
if(l==r)
T[k].mx=w;
else
{
build(LC(k),l,T[k].mid);
build(RC(k),T[k].mid+1,r);
pushup(k);
}
}
void update(int k,int val)
{
if(T[k].l==T[k].r&&T[k].mx>=val)
{
printf("%d\n",T[k].l);
T[k].mx-=val;
}
else
{
if(T[LC(k)].mx>=val)
update(LC(k),val);
else
update(RC(k),val);
pushup(k);
}
}
int main(void)
{
int i,wid;
while (~scanf("%d%d%d",&h,&w,&n))
{
int R=min(h,n);
build(1,1,R);
for (i=0; i<n; ++i)
{
scanf("%d",&wid);
if(T[1].mx<wid)
puts("-1");
else
update(1,wid);
}
}
return 0;
}
上一篇:一个关于多线程和DbHelper的问题


下一篇:Java 并发系列之四:java 多线程