无法解决作业(ACM 培训)
我不知道如何解决这个问题: http://acm.sgu.ru/problem.php?contest=0& ;problem=311
请帮我解决这个问题,
我知道可以使用线段树来解决,但我不知道如何解决
I have no idea how to solve this:
http://acm.sgu.ru/problem.php?contest=0&problem=311
Please help me with this one
I know it can be solved using segment tree but I don't know how
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
读取所有价格并建立线段树。对于每个段,存储价格属于该段的件数和总成本。这就是大部分问题,这个答案的其余部分将相当模糊,希望您能学到一些东西。
处理片段的到达是线段树中直接的 O(log n) 时间下降。
处理购买请求也是一个 O(log n) 时间下降过程,如果进行了销售,则随后进行更新。更新可能会遍历大量的线段树,并且仅在摊销意义上才算快 - 当且仅当存在该价格范围内的片段时才应输入间隔,并且应将其移除的运行时间计入到达时间.
Read all of the prices and set up a segment tree. For each segment, store the number and total cost of pieces whose prices lie in that segment. That's most of the problem, and the rest of this answer will be rather vague in hopes that you learn something.
Handling an arrival of pieces is a straightforward O(log n)-time descent in the segment tree.
Handling a purchase request is also an O(log n)-time descent followed by an update if a sale was made. The update may traverse a large amount of the segment tree and is fast only in an amortized sense – an interval should be entered if and only if there are pieces in that price range, and the running time of their removal should be charged to the arrival.
每行循环:
Loop per line:
我不得不说,我并不认为线段树是解决这个问题的最佳方案。它们是否是“最佳”数据结构似乎取决于到达请求与购买请求的比率,因为每次进行销售时,在删除段后都会有相当多的工作来更新段树。
如果您将库存存储为链接列表,并且每个节点包含以特定单位成本出售的商品数量,该怎么办?这将使插入和移除库存的成本大大降低。要检查是否可以进行销售,您必须迭代 while 循环,累积成本和数量,直到达到目标或超过成本。这里的优点是,如果您进行销售,那么您停止的节点现在是您想要开始下一次搜索的库存的最低成本。如果您使用垃圾收集语言,您只需将对列表头部的引用更改为您停止的节点即可使代码简洁。
当新库存到达时,单位成本插入最坏的情况是 O(n),其中 n 是到达的数量。我认为在现实世界中,这不会是一个糟糕的系统,因为真正的企业会期望大量的销售(满意)与非销售(不满意)。如果有很多人想要购买几乎全部库存,但只是缺少资金来实现这一目标,那么这种方法的效果会很差。
I have to say that I am not sold on segment trees as the best solution to this problem. Whether they are the "best" data structure seems to depend on the ratio of ARRIVE to BUY requests, because each time you make a sale, there is going to be quite a lot of work to update your segment tree after the removal of a segment.
What if you stored your inventory as a linked-list and each node contained the number of items for sale at a particular per-unit cost. This would make the cost of inserting and removing inventory much lower. To check whether you can make a sale you have to iterate through a while loop accumulating cost and quantity until you meet the goal or exceed the cost. The advantage here is that, if you make a sale, then the node you stop on is now the lowest cost of piece of inventory with which you want to start your next search. If you are working in a garbage collecting language you can just change the reference to the head of your list to the node you stopped on which would make for succinct code.
On an arrival of new inventory with a unit cost insertion is at worst O(n) where n is the # of arrivals you have. I think in the real world this wouldn't be a bad system because a real business would expect a high number of sales (happy) to non-sales (unhappy). Where this approach would perform poorly is if there were many people that wanted to buy almost your whole inventory but were just a little short on money to accomplish it.
我刚才凭空写下了这个(15 分钟)。仅对示例中的 100k 次重复查询进行测试,并且需要 1 秒。
请发表评论,因为这可能是废话:
编辑:添加评论
I wrote this out of my head just now (15min). Only testet with 100k repeated queries from the example, and with that it takes 1s.
please comment cause it might be bullshit:
EDIT: added comments