hdu 2795 Billboard(线段树-单点更新)
2015-08-04 09:52
495 查看
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
Sample Output
题意:有一块尺寸为h*w的矩形长板,要在上面贴1*wi的海报n张,选择贴海报的位置是:尽量高,同一高度,选择尽量靠左的地方。要求输出每张海报的高度位置。
因为海报的高都是1,所以最高的高度就是n,题目要求海报尽可能的高,所以用高来建树最好,每个高都对应一个宽,初始的时候每个高的宽都是w,区间维护最大的宽,当进行更新操作的时候,先判断区间维护的最大宽合不合适,合适的情况下,再缩小区间(也就是尽可能贴高处),再判断,直到区间收缩成一个点,然后更新其宽,再往上更新。
#include <iostream>
#include <stdio.h>
using namespace
std;
#define maxn 200002
int Max[3*maxn];
int h,w,n;
void PushUp(int root)
{
Max[root]=max(Max[root<<1],Max[(root<<1)+1]);
}
void Build(int L,int R,int root)
{
Max[root]=w;//初始都为w
if(L==R)
return;
int m=(L+R)>>1;
Build(L, m, root<<1);
Build(m+1, R, (root<<1)+1);
}
int Query(int x,int L,int R,int root)//查询+更新
{
if(L==R)
{
Max[root]-=x;
//更新宽
return L;
}
int m=(L+R)>>1;
int ans=0;
if(Max[root<<1]>=x)//先往高处贴
ans=Query(x,L,m,root<<1);
else
ans=Query(x,m+1,R,(root<<1)+1);
PushUp(root);
//更新
return ans;
}
int main()
{
int x;
while (scanf("%d%d%d",&h,&w,&n)!=EOF)
{
if(h>n)
h=n;
Build(1,h,1);
for (int i=0; i<n; i++)
{
scanf("%d",&x);
if(Max[1]<x)
{
puts("-1");
continue;
}
printf("%d\n",Query(x,1,h,
1));
}
}
return 0;
}
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
题意:有一块尺寸为h*w的矩形长板,要在上面贴1*wi的海报n张,选择贴海报的位置是:尽量高,同一高度,选择尽量靠左的地方。要求输出每张海报的高度位置。
因为海报的高都是1,所以最高的高度就是n,题目要求海报尽可能的高,所以用高来建树最好,每个高都对应一个宽,初始的时候每个高的宽都是w,区间维护最大的宽,当进行更新操作的时候,先判断区间维护的最大宽合不合适,合适的情况下,再缩小区间(也就是尽可能贴高处),再判断,直到区间收缩成一个点,然后更新其宽,再往上更新。
#include <iostream>
#include <stdio.h>
using namespace
std;
#define maxn 200002
int Max[3*maxn];
int h,w,n;
void PushUp(int root)
{
Max[root]=max(Max[root<<1],Max[(root<<1)+1]);
}
void Build(int L,int R,int root)
{
Max[root]=w;//初始都为w
if(L==R)
return;
int m=(L+R)>>1;
Build(L, m, root<<1);
Build(m+1, R, (root<<1)+1);
}
int Query(int x,int L,int R,int root)//查询+更新
{
if(L==R)
{
Max[root]-=x;
//更新宽
return L;
}
int m=(L+R)>>1;
int ans=0;
if(Max[root<<1]>=x)//先往高处贴
ans=Query(x,L,m,root<<1);
else
ans=Query(x,m+1,R,(root<<1)+1);
PushUp(root);
//更新
return ans;
}
int main()
{
int x;
while (scanf("%d%d%d",&h,&w,&n)!=EOF)
{
if(h>n)
h=n;
Build(1,h,1);
for (int i=0; i<n; i++)
{
scanf("%d",&x);
if(Max[1]<x)
{
puts("-1");
continue;
}
printf("%d\n",Query(x,1,h,
1));
}
}
return 0;
}
相关文章推荐
- 清除android程序中的多余资源
- 【从头开始写操作系统系列】环境搭建以及第一个Hello World
- iOS网络——NSURLCache设置网络请求缓存
- DEDE 调用导航栏的两种方法
- ios-疯狂猜图
- js中获取 <%=request.getContextPath()%>
- Javascript模块化编程(二):AMD规范
- Viewpager和fragment和自定义的底部Tab实现切换效果
- liquibase安装
- iOS中的文件管理操作
- SQL 已更新或删除的行值要么不能使该行成为唯一行(sqlserver中有多行所有的列重复的数据,无法更新或删除问题)
- Velocity判断空的方法
- Java的HashMap和HashTable
- python3入门之赋值语句介绍
- 4418开发板的使用方法
- 南邮 OJ 1052 水王争霸
- linux内核编译过程的最终总结版
- poj3440--Coin Toss(几何上的概率)
- Mantis 报表中文乱码
- Difference among Field,Properties and Attributes in Programming laguages