mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Billboard

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


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
 

Recommend
lcy

一開始看到题目时。仅仅扫了一眼。满满的全是英文,看不下去,直接pass,看其它题去了。

之后看到出这题的人越来越多。没办法还是硬着头皮開始读题。

看完之后,直接想到线段树维护区间最小值。敲起来也挺顺的,比想象中要简单。O(∩_∩)O哈!看来以后还是不能由于题目太长难读,而放弃AC机会了。


题意:有一块尺寸为h*w的宣传栏,要在上面贴1*wi的宣传单n张,选择的位置是要求为:尽可能高,对于同一高度,则选择尽可能靠左的地方。要求输出每张宣传单所在行的高度。



思路:线段树维护区间最小值,以1~h划分区间,每个区间中保存该区间已经被占用的最小长度,每次从最上面開始找。找到符合的位置贴上,该位置减去wi。更新区间的最小长度值。


AC代码例如以下:

/*
Author:ZXPxx
Memory: 6320 KB		Time: 2698 MS
Language: C++		Result: Accepted
*/


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,(rt<<1)|1
#define root 1,n,1
#define mid (l+r)>>1
#define LL  long long

const int MX = 3e5+5 ;
int w,h,n;
int sum[MX<<2];
void pushup(int rt) {
    sum[rt]=min(sum[rt<<1],sum[rt<<1|1]);
}
void build(int l,int r,int rt) {
    memset(sum,0,sizeof(sum));
}
int query(int x,int l,int r,int rt) {
    if(l==r) {
        sum[rt]+=x;
        return l;
    }
    int m=mid,ret=0;
    if((sum[rt<<1])+x<=w) ret=query(x,lson);
    else
        ret=query(x,rson);
    pushup(rt);
    return ret;
}
int main() {
    int x;
    while(~scanf("%d%d%d",&n,&w,&h)) {
        n=min(h,n);
        build(root);
        for(int i=1;i<=h;i++) {
            scanf("%d",&x);
            if(sum[1]+x>w)
                printf("-1\n");
            else
                printf("%d\n",query(x,root));
        }
    }
    return 0;
}


posted on 2017-08-14 08:43  mthoutai  阅读(220)  评论(0编辑  收藏  举报