HDU 2795 单点更新,区间优先查找(想法)
Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13382 Accepted Submission(s): 5770
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.
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.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 200005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n;
int b[N];
int h, w;
int ans[N]; struct node{
int l, r, minw;
}a[N*]; void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].minw=;
if(l==r) return;
build(l,mid,ll);
build(mid+,r,rr);
}
int flag; void solve(int id,int root){
if(b[id]+a[root].minw>w) return; //若这个区间最小的高度+要贴报纸的高度大于木板的高度则不能贴
if(a[root].l==a[root].r){
ans[id]=a[root].l;
a[root].minw+=b[id];
flag=;
return ;
}
if(b[id]+a[ll].minw<=w) solve(id,ll); //优先贴最左边
else solve(id,rr);
a[root].minw=min(a[ll].minw,a[rr].minw);//向上更新
} main()
{
int i, j, k;
while(scanf("%d %d %d",&h,&w,&n)==){
for(i=;i<=n;i++) scanf("%d",&b[i]);
build(,min(n,h),);
for(i=;i<=n;i++){
if(b[i]>w){ //若b[i]>w,显然不能贴
ans[i]=-;continue;
}
flag=;
solve(i,);
if(!flag) ans[i]=-; //若不能贴
}
for(i=;i<=n;i++) printf("%d\n",ans[i]);
}
}
HDU 2795 单点更新,区间优先查找(想法)的更多相关文章
- hdu 1166 (单点更新+区间求和+裸题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...
- HDU 1754 I Hate It(线段树之单点更新 区间最值查询)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)
HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- LightOJ 1112 Curious Robin Hood (单点更新+区间求和)
http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i 将第i个数值输出,并将第i个值清0 2 i v ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- HDU 3308 LCIS(线段树单点更新区间合并)
LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...
随机推荐
- 无限极分类,传递一个子ID得到所有父集,用于在前台分层显示标题
方法: static public function getParents($data,$id){ $arr=array(); foreach ($data as $v) { if ($v['id'] ...
- 新建我的 第一个maven项目
maven是管理项目的,myeclipse是编写代码的.第一次写项目都要配置好多东西,很麻烦,now 来看看怎样新建一个maven项目. 百度经验:jingyan.baidu.com 工具/原料 ...
- JavaWeb学习总结(一)—JavaWeb开发入门及环境搭建
一.基本概念 1.1.软件体系结构 1.C/S:Client/Servlet,例如QQ就是CS结构需要编写服务器端程序和客户端程序.缺点:更新需要两端,总要求客户下载新的客户端程序优点:安全性比较好2 ...
- linux之稀疏文件
1. Sparse 文件是并不占用磁盘存储空间. 2. rm 某文件后, 文件占用的磁盘空间并不是立即释放, 而是其句柄没有被任意一个进程引用时才回收. 3. ls 的结果是 apparent siz ...
- 最大的LeftMax与rightMax之差绝对值
这两天去 牛客网 混了下,遇到的几道题都很有意思,尤其是今晚这道,比赛时不会做,后来看了别人的代码才突然想通的,题目链接: 最大的LeftMax与rightMax之差绝对值,大意是: 想了一晚都没想出 ...
- C++—函数探幽
一.内联函数 1.内联函数的机制 内联函数是C++为提高程序运行速度而做的一项改进. 函数调用机制:常规函数调用使程序使程序跳到被掉函数的地址,并在函数结束时返回. 内联函数的机制:内联函数的代码与其 ...
- js知识点 知识树 知识结构 (转载 学习中)
- 转:switch内部的变量定义问题(goto类似)
自我总结:(之前查过goto和switch的资料但是一直没有搞懂,直到今天看到这个讨论才懂了) 1 int a; 是个描述,而不是个命令,只是说明我需要空间,编译器会保证在相应的作用域之中这 ...
- linux 静态库、共享库
http://blog.chinaunix.net/uid-26833883-id-3219335.html http://blog.chinaunix.net/uid-23069658-id-314 ...
- SDL1.2到2.0的迁移指南(转)
里面有些单词不好翻译所以放在开头,以备查验. BLock Image Transfer, a computer graphics operation in which two bitmap patte ...