Arrange seat of a bench for people
Given a bench with n seats and few people sitting, tell the seat number each time when a new person goes to sit on the bench such that his distance from others is maximum?
G jia
A few clarifying questions that i would ask:
1. What if the bench is emtpy. Can i return any seat number?
2. What if bench is full. Should i return -1.
3. How big can n be?
4. How many queries are we making?
Algo:
1. Traverse the bench once (saved as array or vector) and populate a max heap based on distance between two occupied seats. For each node in heap have distance, start seat no and end seat no.
2. When a query is made return the seat no as (end seat no - start seat no)/2 for the root node in heap.
3. Replace the root node with one node formed by (end seat no - start seat no)/2 and end seat no and call heapIfY() on heap.
4. insert the seconds node formed by start node no and (end seat no - start seat no)/2 in the heap.
Repeat process from 2 to 4 for each query.
Complexity:
Forming heap: O(n)
For each query: O(log(n))
Arrange seat of a bench for people的更多相关文章
- 2018SDIBT_国庆个人第三场
A - A CodeForces - 1042A There are nn benches in the Berland Central park. It is known that aiai peo ...
- CodeForce--Benches
A. Benches There are nn benches in the Berland Central park. It is known that aiai people are curr ...
- Codeforces Round #510 (Div. 2) A&B By cellur925
第一次CF祭== 由于太菜了只做了前两题== 因为在第一题上耗费时间太多了,我还是太菜了==. A. Benches time limit per test 1 second memory limit ...
- Codeforces Round #460 (Div. 2)-C. Seat Arrangements
C. Seat Arrangements time limit per test1 second memory limit per test256 megabytes Problem Descript ...
- Codeforces 919 C. Seat Arrangements
C. Seat Arrangements time limit per test 1 second memory limit per test 256 megabytes input standa ...
- codeforces 919C Seat Arrangements 思维模拟
C. Seat Arrangements time limit per test 1 second memory limit per test 256 megabytes input standard ...
- MySQL可视化软件Work Bench导出导入数据库
首先打开你的work bench,输入你的密码进入主页面 A:导入数据库 在Schemas空白处右键选择Create~:建立一个数据库,然后就可以导入你的sql文件了 File-->Open S ...
- bench.sh 跑分测速
#!/bin/bash #==============================================================# # Description: bench te ...
- 通过Measure & Arrange实现UWP瀑布流布局
简介 在以XAML为主的控件布局体系中,有用于完成布局的核心步骤,分别是measure和arrange.继承体系中由UIElement类提供Measure和Arrange方法,并由其子类Framewo ...
随机推荐
- python3 基础二——基本的数据类型二
一.数字(Number) 1.Python支持三种不同的数值类型:整型(int),浮点型(float),复数(complex) 2.Python数字数据类型用于存储数值 3.数据类型是不允许改变的,这 ...
- ggplot2绘制Excel所有图
出处:https://brucezhaor.github.io/blog/2016/06/13/excel2ggplot/#%E5%89%8D%E8%A8%80 目录 前言 1.用到的包 2.数据准备 ...
- npm常规操作
1.如果想在当前文件下创建package.json,只要在当前目录下执行npm init 2.如果执行过程中缺少依赖,有两种途径 ①安装全局 npm install -g express ②在当前工程 ...
- 处理 read_csv 报错 OSError:Initializing from file failed
1.问题发现 df=pd.read_csv("X-go报表_交易20191118.csv") print(df.info()) File "pandas/_libs/pa ...
- Spring boot dubbo+zookeeper 搭建------基于gradle项目的消费端与服务端分离实战
1. Dubbo简介 Dubbo是Alibaba开源的分布式框架,是RPC模式的一种成熟的框架,优点是可以与Spring无缝集成,应用到我们的后台程序中.具体介绍可以查看Dubbo官网. 2. Why ...
- C语言学习系列(六)基本语法
一.C运算符 算术运算符(语法和java类似或基本一样略过不再描述) 关系运算符(略) 逻辑运算符(略) 位运算符 运算符 描述 实例 & 如果同时存在于两个操作数中,二进制 AND 运算符复 ...
- jQuery.proxy(function,context)
jQuery.proxy(function,context) 概述 jQuery 1.4 新增.返回一个新函数,并且这个函数始终保持了特定的作用域.大理石平台检定规程 当有事件处理函数要附加到元素上, ...
- Oracle 后台进程(三)LGWR进程
一.LGWR进程简介 LGWR,是Log Writer的缩写,也是一种后台进程.主要负责将日志缓冲内容写到磁盘的在线重做日志文件或组中.DBWn将dirty块写到磁盘之前,所有与buffer修改相关的 ...
- 定时器TIM,pwm
一.定时器 1. 定义 设置等待时间,到达后则执行指定操作的硬件. 2. STM32F407的定时器有以下特征 具有基本的定时功能,也有PWM输出(灯光控制.电机的转速).脉冲捕获功能( ...
- pat 甲级 1057 Stack(30) (树状数组+二分)
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...