无法解决作业(ACM培训)

时间:2011-02-28 12:13:13

标签: algorithm binary-tree segment-tree

我不知道如何解决这个问题: http://acm.sgu.ru/problem.php?contest=0&problem=311

请帮我解决这个问题

我知道它可以使用分段树来解决,但我不知道如何

4 个答案:

答案 0 :(得分:4)

  1. 阅读所有价格并设置细分树。对于每个细分,请存储价格位于该细分中的作品的数量和总成本。这是问题的大部分,而这个答案的其余部分将是相当模糊的,希望你能学到一些东西。

  2. 在段树中处理碎片的到达是一个简单的O(log n)时间下降。

  3. 处理购买请求也是O(log n)时间下降,然后在进行销售时进行更新。更新可以遍历大量的分段树,并且仅在分期付款的意义上是快速的 - 当且仅当在该价格范围内有件时才应输入间隔,并且应当在到达时将其移除的运行时间收费

答案 1 :(得分:2)

每行循环:

  • 使用参数(ARRIVE / BUY)解释行命令
  • 更新型号(每个价格的冰淇淋数量),型号:std :: map price to quantity。
  • 在购买的情况下,输出是否可以出售足够的冰淇淋,先销售最便宜的冰淇淋(首先在地图上)。

答案 2 :(得分:0)

我不得不说我不是在细分树上出售这个问题的最佳解决方案。它们是否是“最佳”数据结构似乎取决于ARRIVE与BUY请求的比率,因为每次进行销售时,在删除段后更新段树将会有相当多的工作。

如果您将库存存储为链接列表,并且每个节点包含特定单位成本的待售商品数,该怎么办?这将使插入和移除库存的成本大大降低。要检查您是否可以进行销售,您必须迭代累积成本和数量的while循环,直到达到目标或超过成本。这样做的好处是,如果您进行销售,那么您停下来的节点现在是您希望开始下一次搜索的最低库存成本。如果您使用的是垃圾收集语言,则只需将列表头部的引用更改为您停止的节点即可生成简洁的代码。

在新单位到达时,单位成本插入最差O(n),其中n是您的到达数量。我认为在现实世界中这不会是一个糟糕的系统,因为真正的企业会期望大量的销售(快乐)到非销售(不快乐)。这种方法表现不佳的地方是,有很多人想要购买几乎所有的库存,但只需要一点钱来完成它。

答案 3 :(得分:0)

我刚刚写了这篇文章(15分钟)。只有来自示例的带有100k重复查询的testet,并且需要1s。

请评论因为它可能是废话:

#!/usr/bin/python
import sys

class store:
    def __init__(self):
        self.sorted = [] # list of tuples (cost, amount) - sorted by cost
        # btw: sorting of tuples is by its elements, so first by element 0
        # that means it is sorted by cost, cheapest first
        self.count = 0 # the global amount of ice in store
        self.mergemap = {} # key = cost, value = tuple of (cost, amount)
        # the mergemap prevents the creation of multiple tuples per priceclass

    def request(self, n, money):
        if self.count < n:
            # if we have less ice in store than was requested -> unhappy
            return False
        pcsleft = n # we count down

        # loop through each priceclass as x
        for x in self.sorted:
            # x[0] is cost, x[1] is the amount of ice of that price in store
            if x[1] < pcsleft and x[0]*x[1] <= money:
                # we found cheap ice, but there is not enough of it
                pcsleft -= x[1]
                money -= x[0]*x[1]
            elif x[1] >= pcsleft and x[0]*pcsleft <= money:
                # theres enough cheap ice, next iteration will not occour
                pcsleft = 0
                money -= x[0]*pcsleft
            else:
                return False # the cheapest ice is too expensive
            if pcsleft == 0 and money >= 0:
                break # happy - break because for-each loop, not while
            if money < 0: # just for kicks, should never happen
                print "error"
                sys.exit(1)
        # when we have cheap enough ice, we remove ice from storage
        # we iterate through the priceclasses like before
        # but when we remove ice from one category, either the loop ends
        # or we completly deplete it  so it can be removed
        while n > 0: # subtract the bought count
            x = self.sorted[0]
            if x[1] > n: # the first priceclass has enough ice
                x[1] -= n
                self.count -= n
                return True # were happy
            elif x[1] == n:
                del(self.mergemap[x[0]]) # remove from mergemap
                self.sorted = self.sorted[1:] # completly remove priceclass
                self.count -= n
                return True
            elif x[1] < n: # 
                n -= x[1]
                self.count -= x[1]
                del(self.mergemap[x[0]])
                self.sorted = self.sorted[1:]

        return True

    def arrive(self, n, cost):
        if cost not in self.mergemap: # we dont have ice in that priceclass
            # create a new tuple, actually list, cause tuples are immutable
            x = [cost, n]
            self.sorted.append(x)
            # resort, should be fast, cause its almost entirely sorted,
            # and python has sorting magic :)
            self.sorted.sort()
            self.mergemap[cost] = x # insert into mergemap
        else:
            # just update the tuple, via its reference in the mergemap
            self.mergemap[cost][1]+=n
            self.count
        self.count += n # keep count correct
        # O(n*logn)

if __name__=='__main__':
    s = store()
    i = 0
    # we read from stdin
    for line in sys.stdin:
        #print line.strip()+" --> ",
        req, count, cost = line.split(' ') # not error tolerant
        if req == 'ARRIVE':
            s.arrive(int(count), int(cost))
        elif req == 'BUY':
            if s.request(int(count), int(cost)):
                print 'HAPPY '+str(i)
            else:
                print 'UNHAPPY '+str(i)
        i+=1
    print s.sorted # print out the left over ice

编辑:添加评论