HDU 2795 单点更新,区间优先查找(想法)

Billboard

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

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
 
 
题目意思:
一个h*w的木板,要在在上面贴n张报纸,每个报纸都是1*w[i]矩形,优先贴在木板的上方和左方,报纸之间不能重叠,若能贴输出所贴位置的h(从上到下1-h),否则输出-1。
 
思路:
暴力肯定超时,需要转换一下思维,若把木板旋转90度呢?问题就变成:给一h*w的矩形,优先往最左边贴报纸,其次往最左面的下边贴报纸。很明显是线段树嘛,维护横着每个单位最小的高度即可。说着不是太好说,看代码吧,代码是最好的解题报告。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 200005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n;
int b[N];
int h, w;
int ans[N]; struct node{
int l, r, minw;
}a[N*]; void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].minw=;
if(l==r) return;
build(l,mid,ll);
build(mid+,r,rr);
}
int flag; void solve(int id,int root){
if(b[id]+a[root].minw>w) return; //若这个区间最小的高度+要贴报纸的高度大于木板的高度则不能贴
if(a[root].l==a[root].r){
ans[id]=a[root].l;
a[root].minw+=b[id];
flag=;
return ;
}
if(b[id]+a[ll].minw<=w) solve(id,ll); //优先贴最左边
else solve(id,rr);
a[root].minw=min(a[ll].minw,a[rr].minw);//向上更新
} main()
{
int i, j, k;
while(scanf("%d %d %d",&h,&w,&n)==){
for(i=;i<=n;i++) scanf("%d",&b[i]);
build(,min(n,h),);
for(i=;i<=n;i++){
if(b[i]>w){ //若b[i]>w,显然不能贴
ans[i]=-;continue;
}
flag=;
solve(i,);
if(!flag) ans[i]=-; //若不能贴
}
for(i=;i<=n;i++) printf("%d\n",ans[i]);
}
}
上一篇:一. Linux 常用命令总结


下一篇:linux vmware安装完成后如何设置桥接上网