algorithm - 摊销成本/插入和删除
问题描述
Design a data structure to support the following two types of operations on an initially empty
set S of real numbers:
— Insert(S,x): Inserts number x into S.
— Delete(S): Deletes the largest n/2 numbers from S.
Explain how to implement this data structure with O(1) amortized cost per operation.
这怎么可能?对我来说,似乎每次调用 Insert 时我都需要对数据结构进行排序,如果它没有排序,则删除将花费超过 O(1) 的时间。因此,插入的摊销成本应如下:
插入的摊销成本 = (S + 1)实际成本 + (S + 1)之后的数据结构 - (S - 1)之前的数据结构 = S + 1
这样,摊销成本显然不是 O(1)
解决方案
推荐阅读
- r - glm.fit(cbind(ones, x[, index], x[, condind]), y, family = gaussian()) 中的 SIS r 错误:“x”中的 NA/NaN/Inf
- r - 如何创建一个 8 x 2 列,将所有重复值和位置分类到 1 个组中?
- javascript - 如何在不显示在调试器上的情况下将秘密数据从 JS 发送到 PHP?
- apache-kafka - Kafka Connect 和 Kafka Broker 版本兼容性
- c++ - 如何修复派生类给出“声明没有存储类或类型说明符”错误?
- c# - 如何检查 Streamread 中每一行的每个第一个字符?
- sql - 使用 NOT 运算符的带有子查询的 SQL 查询
- windows - 我将 pkg-config 设置为路径和堆栈仍然给我文件未找到错误。窗户 10
- c# - 如何从 JSON 文本中解析特定部分以制作英雄联盟中的英雄列表
- python - 通过在python中将数据结构写入XML文件的TypeError