1. 程式人生 > >HDU 2795 Billboard 線段樹 java實現

HDU 2795 Billboard 線段樹 java實現

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
3 5 5
2
4
3
3
3
Sample Output
1
2
1
3
-1

題意是有一塊長h寬w的板子,你可以往上貼廣告,位置優先越靠上。
max維護一個剩餘最大的空間,若最大空間Max[1]小於廣告的寬度,則直接返回-1.
線段樹搜尋第一個大於等於w值的位置,query和update寫一起。
注意h的值,線段樹開的空間應該根據n來開。
而且輸入規模過大,不要用Scanner,用BufferedReader輸入。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
    public
static int[] max; public static void pushUp(int rt){ max[rt]=Math.max(max[rt<<1],max[rt<<1|1]); } public static void build(int l,int r,int rt,int val){ max[rt]=val; if(l==r)return; int mid=(l+r)>>1; build(l,mid,rt<<1,val); build(mid+1
,r,rt<<1|1,val); } public static int query(int w,int l,int r,int rt){ if(l==r){ max[rt]-=w; return l; } int mid=(l+r)>>1; int ret; if(max[rt<<1]>=w) { ret=query(w,l,mid,rt<<1); }else{ ret=query(w,mid+1,r,rt<<1|1); } pushUp(rt); return ret; } public static void main(String[] args) throws IOException { //Scanner cin=new Scanner(System.in); BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); while (true){ String[] tmp; try{ tmp=reader.readLine().split("\\s+"); }catch (Exception e){ return; } int h,w,n; h=Integer.parseInt(tmp[0]); w=Integer.parseInt(tmp[1]); n=Integer.parseInt(tmp[2]); h=Math.min(h,n); if(h!=1){ max=new int[h<<2]; }else{ max=new int[10]; } build(1,h,1,w); for(int i=1;i<=n;++i){ int q=Integer.parseInt(reader.readLine()); if(max[1]<q){ System.out.println("-1"); }else { System.out.println(query(q,1,h,1)); } } } } }

相關推薦

HDU 2795 Billboard 線段 java實現

Problem Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and

HDU 2795 Billboard (線段+貪心)

無法 說明 names 枚舉 for 存在 hdu problem detail 手動博客搬家:本文發表於20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/article/details/77488127 URL:

AVL(Java實現)

height sea postorder int void brush node 情況 裏的 AVL樹基本操作 未完....待續.... AVL樹代碼 public class AVLTree<Key extends Comparable<? supe

hdu 2795 Billboard

close HERE it is scrip include build aps tip opened hdu 2795 Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/327

HDU-6070-二分+線段

else 最小 print math src 技術 date time integer Dirt Ratio Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Oth

數據結構實現(四)二叉查找java實現

.com ML treenode 設置 AC getparent 邏輯圖 技術分享 ldb 轉載 http://www.cnblogs.com/CherishFX/p/4625382.html 二叉查找樹的定義:   二叉查找樹或者是一顆空樹,或者是一顆具有以下特性的非空二

Tunnel Warfare HDU - 1540(線段最長連續區間)

ont div n) pan Go build lse ons #define 題意: 一條線上的點,D x是破壞這個點,Q x是表示查詢以x所在的最長的連續的點的個數,R是恢復上一次破壞的點。 解析: 線段樹結點 設置一個 lq記錄區間左端點開始的最大連續個數,

Can you answer these queries? HDU - 4027(線段+技巧)

fin 題意 ios fff PE sqrt += 長度 scan 題意:給一個數組序列, 數組長度為100000 兩種操作: 一種操作是將某一個固定區間所有數開方(向下取整) 另一種操作是詢問某個區間的所有數字之和。 由於數不超過263,因此開個七八次就變成1,由於只有

hdu 1542 Atlantis (線段+掃描線)

over down first uniq sed spa 問題 rec style Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot

HDU 4027(線段

提前 線段樹 都是 ace href 操作 代碼 處理 限制 HDU4027 題意:操作指令為0時,對區間[x,y]之間的數字進行開平方;指令為1的時候,對區間[x,y]之間的數字求和並輸出; 思路:線段樹處理就OK了,但是64位內的數最多開8次平方就為1了(開始不信,試了

LCIS HDU - 3308(線段

Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting from 0) Q A B: output the length of the longest conse

L - Vases and Flowers HDU - 4614(線段

Problem Description   Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some

K - Transformation HDU - 4578(線段

Yuanfang is puzzled with the question below: There are n integers, a 1, a 2, …, a n. The initial values of them are 0. There are four kinds of operati

J - Assign the task HDU - 3974(線段 + dfs序)

There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole compa

輸入某二叉的前序遍歷和中序遍歷的結果,請重建出該二叉(java實現並測試)

假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。 package ssp; class TreeNode { int val; TreeNod

HDU 1540(線段+區間合並)學習記錄

div uil hdu 1540 hup fin bsp play names http 學習了線段樹的新姿勢,記錄一下 參考blog:https://blog.csdn.net/sunyutian1998/article/details/79618316 query的

HDU 1540(線段+區間合併)學習記錄

學習了線段樹的新姿勢,記錄一下 參考blog:https://blog.csdn.net/sunyutian1998/article/details/79618316 query的時候m-ql+1和qr-m寫成了m-l+1、r-m,wa了幾發之後才找到bug 錯誤樣例: 10 1Q 5 wrong

Interviewe HDU - 3486【線段+二分搜尋】

題目連線   題意是這樣說的,有N個人去面試,他們有各自的價值,然後大BOSS又比較的忙,所以想要派出M個小老闆去稽核這幾個面試的小老弟,但是每個小老闆只能夠選出一個人來,就問要達到價值K,需要幾個小老闆選出的最出色的小老弟。   看到這道題之後,我就想,我們可以用線

最優二叉搜尋(JAVA實現

演算法課上老師講的最優二叉搜尋樹,自己又查了些資料才看明白。這篇只記錄下自己用java的實現和自己的一些細節的理解。想學習整個演算法的可以參照 https://blog.csdn.net/zhangyifei521/article/details/50833792 package bes

HDU 1540(線段區間合併+維護連續0或1)

題意:1-n個地道,m個次操作,D代表摧毀第i個地道,Q代表查詢包含第i個地道的最大連續地道數目,並輸出。R代表修復最近摧毀的那個地道; 思路:這題利用了線段樹的相鄰結點區間是連續的特性,而且是維護連續的1,所以用llen、rlen、len陣列分別表代表當前結點的1的最長連續字首長、最長連續後綴