1 2 3 4 5 6 7 8 9 10 11 | python req.py 0 1 20 1 2 30 Solution: [20, 30] python req.py 0 2 10 1 3 5 Solution: [5, 5, 1] python req.py 0 2 100 1 3 50 Solution: [46, 54, 6] python req.py 0 2 100 1 3 50 2 3 20 Solution: [49, 51, 21] |
1 2 3 4 5 6 7 | class Manager: def __init__(self): self.rangeReqs = [] self.reqs = None def addRangeReq(self, rangeReq): self.rangeReqs += [rangeReq] |
1 2 3 4 5 | class RangeSize: def __init__(self, be=(0,1), sz=1): self.begin = be[0] self.end = be[1] self.size = sz |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | def solve(self): n = self.nSegments() m = len(self.rangeReqs) self.reqs = n * [0] self.rangeReqs.sort() # Satisfy single segment requirements dumSingle = RangeSize((n, n+1), 1) endSingleIndex = bisect.bisect_right(self.rangeReqs, dumSingle) for rr in self.rangeReqs[ : endSingleIndex]: curr = self.reqs[rr.begin] needMore = rr.size - curr self.reqs[rr.begin] += needMore bigRangeReqs = self.rangeReqs[endSingleIndex:] # non-single ranges self.partialSatisfy(bigRangeReqs, 1, 2) # half self.partialSatisfy(bigRangeReqs, 1, 2) # half self.partialSatisfy(bigRangeReqs, 1, 1) # complete return self.reqs |
1 2 3 4 5 6 7 8 9 10 11 12 | def partialSatisfy(self, bigRangeReqs, rationN, ratioD): # Thru reqs, add to satisfy rationN/ratioD of requirement for rr in bigRangeReqs: curr = sum(self.reqs[rr.begin: rr.end]) needMore = rr.size - curr if needMore > 0: give = (rationN * (needMore + ratioD - 1)) / ratioD q, r = divmod(give, rr.end - rr.begin) for si in range(rr.begin, rr.end): self.reqs[si] += q for si in range(rr.begin, rr.begin + r): self.reqs[si] += 1 |
1 2 3 4 | GTK_table_size_request_pass1 (table); GTK_table_size_request_pass2 (table); GTK_table_size_request_pass3 (table); GTK_table_size_request_pass2 (table); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |