【线段树求最靠前】【HDU2795】【Billboard】
题意:
有一个H*W的广告牌,当插入一个广告时(1*Wi),问最靠前的插入方式是什么
新生赛有个类似的题目,可惜当时居然没水过去。
果断用线段树做 以H为线段 建树,存[l,r]中最大的宽度,因为区间最大值满足区间和性质。
所以线段树几个要素如下:
线段:H
区间和性质:最大值
代码:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 222222
using namespace std;
int h,w,n;
int tree[maxn*4];
int A[maxn];
void PushUp(int rt)
{
tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int build(int l,int r,int rt)
{
if(l==r) {tree[rt]=w;return 0;}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
} void input()
{
for(int i=1;i<=n;i++)
scanf("%d",&A[i]);
}
int updata(int p,int k,int l,int r,int rt)
{
int m;
if(l==r) {tree[rt]+=k;return 0;}
m=(l+r)>>1;
if(p<=m) updata(p,k,lson);
else updata(p,k,rson);
PushUp(rt);
}
int query(int p,int l,int r,int rt)
{
if(p>tree[rt]) return -1;
if(l==r) return l;
int m=(l+r)>>1;
if(p<=tree[rt<<1]) return query(p,lson);
else return query(p,rson);
}
void solve()
{
for(int i=1;i<=n;i++)
{
int t=query(A[i],1,h,1);
printf("%d\n",t);
if(t!=-1)
{
updata(t,-A[i],1,h,1);
}
}
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int main()
{
// init();
while(scanf("%d%d%d",&h,&w,&n)!=EOF)
{
memset(tree,0,sizeof(tree));
h=min(h,200000);
build(1,h,1);
input();
solve();
}
return 0;
}
【线段树求最靠前】【HDU2795】【Billboard】的更多相关文章
- xdoj-1324 (区间离散化-线段树求区间最值)
思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i] 覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- UVA 11983 Weird Advertisement --线段树求矩形问题
题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...
- BNU 2418 Ultra-QuickSort (线段树求逆序对)
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...
- hdu 1394 (线段树求逆序数)
<题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...
- 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)
题目链接:http://codevs.cn/problem/4163/ 题目:
- poj2299 Ultra-QuickSort(线段树求逆序对)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)
链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...
- HDU_1394_Minimum Inversion Number_线段树求逆序数
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- 解决ios上微信无法捕获返回键按钮事件的问题
1 //匿名函数 $(function(){ getHistory(); var flag=false; setTimeout(function(){ flag=true },1000) window ...
- STL中的set集合容器进行集合运算:并、交、差实例
集合容器的集合运算:并.交.差: #include "stdafx.h" #include <iostream> #include <set> #inclu ...
- MapReduce程序依赖的jar包
难得想写个mapreduce程序.发现已经不记得须要加入那些jar包了,网上找了一会也没发现准确的答案.幸好对hadoop体系结构略知一二.迅速试出了写mapreduce程序须要的五个jar包. 不多 ...
- Linux SSH 远程操作与传送文件
操作系统:centos 6.5 x64 一.远程连接:在进行linux 的 ssh远程操作前,一定要确认linux 是否安装了 openssh-clients,为了方便起见,一般用yum安装即可:# ...
- Android设置背景
一.设置图片背景 首先你先将一个的背景图片存入工程中res/drawble(当然drawble-hdpi.drawble-mdpi.drawble-ldpi中一个或者几个文件夹都可)文件夹中.假如我存 ...
- escape和unescape给字符串编码
var before = "\xxx\xxx" var after = escape(before); var after2 = unescape(after );
- 【常用小命令】解决windows下有些文件文件名识别不了导致删除不了的问题
在百度上找的解决方案哈,只为自己存档一份. 因为发现现在从csdn上下载的文件都是“.pdf_”格式,下载2个文件,将一个文件格式改成 “.pdf”,另一个文件就扔回不了回收站了, 所以没有办法就找各 ...
- select count(distinct a)
我想对一个表里面字段a的个数进行进行统计,因为字段a有重复的记录,我想排除重复的记录,该sql语句为: select count(distinct a) 链接:http://www.w3school. ...
- JQ 动态添加节点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- isNUll ,对于sql中的一个表的description的NULL和空格的处理
select client.clientname ,description,'client2'= case when client.Description IS NULL then client.c ...