标签:des style blog class code java tar ext javascript width color
Billboard
Time Limit:8000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
Sample Output
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #define MAX 200010 using namespace std; int h,w,n,a; struct Node { int l,r; int len; } Tree[MAX*4]; void build(int l,int r,int x) { Tree[x].l=l; Tree[x].r=r; Tree[x].len=w;//初始化的时候空格的长度都是w if(Tree[x].l==Tree[x].r) return; int mid=(l+r)>>1; build(l,mid,2*x); build(mid+1,r,2*x+1); Tree[x].len=Tree[2*x].len>Tree[2*x+1].len?Tree[2*x].len:Tree[2*x+1].len;//更新最大值 } int query(int num,int x) { if(Tree[x].l==Tree[x].r) { Tree[x].len-=num;//更新线段树最底层的值 return Tree[x].l;//返回行数 } int ans; if(num<=Tree[2*x].len)//小于左子树的长度,访问左子树 ans=query(num,2*x); else if(num<=Tree[2*x+1].len) ans=query(num,2*x+1); Tree[x].len=Tree[2*x].len>Tree[2*x+1].len?Tree[2*x].len:Tree[2*x+1].len;//向上更新父节点的值 return ans; } int main() { while(scanf("%d%d%d",&h,&w,&n)!=EOF) { a=h<n?h:n;//只需要处理n和h中最小的行数 build(1,a,1); int x; while(n--) { scanf("%d",&x); if(x>Tree[1].len) //如果输入的广告长度比剩余最大的宽度还要大,则无解 printf("-1\n"); else printf("%d\n",query(x,1)); } } return 0; }
线段树入门(Billboard),码迷,mamicode.com
标签:des style blog class code java tar ext javascript width color
原文地址:http://www.cnblogs.com/acmer-jsb/p/3702870.html