HDU

来源:互联网 发布:java计算器图形界面 编辑:程序博客网 时间:2024/06/05 05:49

题目大意: 
有一块h*w的矩形广告板,要往上面贴广告; 
然后给n个1*wi的广告,要求把广告贴上去; 
而且要求广告要尽量往上贴并且尽量靠左; 
求第n个广告的所在的位置,不能贴则为-1; 
 
算法思想: 
利用线段树可以求区间的最大值; 
将位置即h用来建树(h<=n,大了没有意义); 
树中存储的为该位置还拥有的空间; 
若左子树的最大值大于他,就查询左子树,否则查询右子树; 

// 线段树#include <iostream>#include <string>#include <algorithm>#define MAX 200010using namespace std;typedef struct {    int l;    int r;    int m;} Node;Node tree[4 * MAX];int w;void build( int root, int l, int r ) {    tree[root].l = l;    tree[root].r = r;    tree[root].m = w;    if( tree[root].l == tree[root].r ) {        return;    }    int mid = ( l + r ) / 2;    build( root * 2, l, mid );    build( root * 2 + 1, mid + 1, r );}int update( int root, int board ) {    if( tree[root].l == tree[root].r ) {        tree[root].m -= board;        return tree[root].l;    }    int ans;    if( tree[root * 2].m >= board ) {        ans = update( root * 2, board );    }    else {        ans = update( root * 2 + 1, board );    }    tree[root].m = max( tree[root * 2].m, tree[root * 2 + 1].m );    return ans;}int main() {    int h, n, board;    while( scanf( "%d%d%d", &h, &w, &n ) != EOF ) {        build( 1, 1, min( h, n ) );        while( n-- ) {            scanf( "%d", &board );            if( tree[1].m < board ) {                printf( "-1\n" );            }            else {                printf( "%d\n", update( 1, board ) );            }        }    }    return 0;}


原创粉丝点击