标签:hdu 2795 billboard 线段树 at the entrance to t there is a huge rect
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2795
~~~~
开始学习数据结构,从简单的开始吧,刷题累了就写解题报告,哈哈,沸腾吧,Segment tree!
~~~~
大致题意:一块 h*w的广告牌,蓝后就是n块1*wi大小的广告,依次贴到广告牌上,而且总是贴到最左上角,问贴完所有广告后所占的高度是多少?
#include<cstdio>
#include<algorithm>
#define lson rt<<1,s,mid
#define rson rt<<1|1,mid+1,e
using namespace std;
const int maxn=222222;
int tre[maxn<<2];
int h,w,n;
void pushup(int rt)
{
tre[rt]=max(tre[rt<<1],tre[rt<<1|1]);
}
void build(int rt,int s,int e)
{
tre[rt]=w;
if(s==e)
return ;
int mid=(s+e)>>1;
build(lson);
build(rson);
}
int query(int rt,int s,int e,int x)
{
if(s==e)
{
tre[rt]-=x;
return s;
}
int mid=(s+e)>>1;
int ans=(tre[rt<<1]>=x)?query(lson,x):query(rson,x);
pushup(rt); //直接把更新操作写到query函数里。
return ans;
}
int main()
{
while(scanf("%d%d%d",&h,&w,&n)==3)
{
if(h>n) h=n; //h>n的话,多余的就空间就浪费掉了,而且貌似会RE
build(1,1,h);
while(n--)
{
int q;
scanf("%d",&q);
if(tre[1]<q) puts("-1"); //不解释了吧~~
else printf("%d\n",query(1,1,h,q));
}
}
return 0;
}
HDU 2795 Billboard.(线段树),布布扣,bubuko.com
标签:hdu 2795 billboard 线段树 at the entrance to t there is a huge rect
原文地址:http://blog.csdn.net/darwin_/article/details/37076091