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的更多相关文章

  1. 2018SDIBT_国庆个人第三场

    A - A CodeForces - 1042A There are nn benches in the Berland Central park. It is known that aiai peo ...

  2. CodeForce--Benches

    A. Benches   There are nn benches in the Berland Central park. It is known that aiai people are curr ...

  3. Codeforces Round #510 (Div. 2) A&B By cellur925

    第一次CF祭== 由于太菜了只做了前两题== 因为在第一题上耗费时间太多了,我还是太菜了==. A. Benches time limit per test 1 second memory limit ...

  4. Codeforces Round #460 (Div. 2)-C. Seat Arrangements

    C. Seat Arrangements time limit per test1 second memory limit per test256 megabytes Problem Descript ...

  5. Codeforces 919 C. Seat Arrangements

    C. Seat Arrangements   time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. codeforces 919C Seat Arrangements 思维模拟

    C. Seat Arrangements time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. MySQL可视化软件Work Bench导出导入数据库

    首先打开你的work bench,输入你的密码进入主页面 A:导入数据库 在Schemas空白处右键选择Create~:建立一个数据库,然后就可以导入你的sql文件了 File-->Open S ...

  8. bench.sh 跑分测速

    #!/bin/bash #==============================================================# # Description: bench te ...

  9. 通过Measure & Arrange实现UWP瀑布流布局

    简介 在以XAML为主的控件布局体系中,有用于完成布局的核心步骤,分别是measure和arrange.继承体系中由UIElement类提供Measure和Arrange方法,并由其子类Framewo ...

随机推荐

  1. js-虚拟dom

    问题: vdom是什么?为什么存在vdom? vdom是如何应用的,核心的api是什么? 介绍一下diff算法 1.一些虚拟dom应用了snabbdom.其中的 h函数相当于渲染成了右侧的JS虚拟节点 ...

  2. 一、vue基础--语法

      用到的前台编程工具是Visual Studio Code,暂时是官网下载vue.js到本地使用 一.Visual Studio Code需要安装的插件: jshint :js代码规范检查 Beau ...

  3. python+requests----登录接口reponse中token传递给其他接口使用的一个简单小示例介绍

    #!/usr/bin/env python # coding=UTF-8 import requests def login(): url = "https://xxxx.xxx.xxx/v ...

  4. Python3基础学习-while循环实例- 猜数字游戏

    需求: 猜数字游戏, 要求如下: 给个数字, 比如是66. 让用户一直猜,直到猜对退出. 中间输入q或Q也能退出 如果猜大了,提示用户猜小一点; 如果猜小了,提示用户猜大一点; likely = 66 ...

  5. vscode python文件注释乱码怎么办?

    VS Code 中文注释显示乱码 解决方法 将设置中的"files.autoGuessEncoding"项的值改为true即可. 1.文件 2.首选项 3.设置 4.搜索 &quo ...

  6. Django REST framework+Vue 打造生鲜电商项目(笔记二)

    (转自https://www.cnblogs.com/derek1184405959/p/8768059.html)(有修改) 接下来开始引入django resfulframework,体现它的强大 ...

  7. 自己实现sizeof+大小端测试

    #define my_sizeof(type) ((char *)(&type+1)-(char*)(&type)) 同时大小端测试 如下 #include <stdio.h&g ...

  8. J-Link OB F103 固件提取及维修

    焊接心得挺不错的,可以学习到了 事情起因 某日在调试stm32的时候,错将5v接入3.3v电源输入,开发板烧掉.而且因为jlink没拔掉,也一同阵亡了.光烧了个芯片把整个板换掉太亏,遂打算动手修复. ...

  9. 20、自动装配-@Autowired&@Qualifier&@Primary

    20.自动装配-@Autowired&@Qualifier&@Primary 自动装配:Spring 利用依赖注入(DI),完成对IOC容器中各个依赖关系赋值 20.1 @Autowi ...

  10. 彻底解决eslint与webstorm针对vue的script标签缩进处理方式冲突问题

    彻底解决eslint与webstorm针对vue的script标签缩进处理方式冲突问题 2018年12月08日 21:58:26 Kevin395 阅读数 1753   背景不多介绍了,直接上代码. ...