2018-10-02 21:24:06 +08:00
|
|
|
''' mbinary
|
|
|
|
#########################################################################
|
|
|
|
# File : splitStripe.py
|
|
|
|
# Author: mbinary
|
|
|
|
# Mail: zhuheqin1@gmail.com
|
2019-01-31 12:09:46 +08:00
|
|
|
# Blog: https://mbinary.xyz
|
2018-10-02 21:24:06 +08:00
|
|
|
# Github: https://github.com/mbinary
|
|
|
|
# Created Time: 2018-08-24 17:07
|
|
|
|
# Description:
|
|
|
|
#########################################################################
|
|
|
|
'''
|
|
|
|
|
2018-08-29 15:52:02 +08:00
|
|
|
'''
|
|
|
|
There is stripe which length is n,
|
|
|
|
priceMap contains a map for different length of stripe and its price
|
|
|
|
then find the maximum price to split the stripe in different shorter stripes
|
|
|
|
( including the original length if possible)
|
|
|
|
'''
|
2020-04-15 12:28:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
def count(n, prices):
|
|
|
|
def best(cur):
|
2018-08-29 15:52:02 +08:00
|
|
|
# note that copying the list or create a new list in the following new_stripes codes
|
2020-04-15 12:28:20 +08:00
|
|
|
if cur in values:
|
|
|
|
return values[cur], stripes[cur]
|
2018-08-29 15:52:02 +08:00
|
|
|
maxPrice = 0
|
2020-04-15 12:28:20 +08:00
|
|
|
new_stripes = []
|
|
|
|
for i, j in prices.items():
|
|
|
|
if i <= cur:
|
2018-08-29 15:52:02 +08:00
|
|
|
p, tmp = best(cur-i)
|
2020-04-15 12:28:20 +08:00
|
|
|
if maxPrice < p+j:
|
|
|
|
# if the list is not copyed, create a new list, don't use append
|
|
|
|
new_stripes = tmp+[i]
|
|
|
|
maxPrice = p+j
|
2018-08-29 15:52:02 +08:00
|
|
|
values[cur] = maxPrice
|
|
|
|
stripes[cur] = new_stripes
|
2020-04-15 12:28:20 +08:00
|
|
|
return maxPrice, new_stripes
|
|
|
|
values = {0: 0}
|
|
|
|
stripes = {0: []}
|
2018-08-29 15:52:02 +08:00
|
|
|
return best(n)
|
|
|
|
|
|
|
|
|
2020-04-15 12:28:20 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
li = [(1, 1), (2, 5), (3, 8), (4, 9), (5, 10),
|
|
|
|
(6, 17), (7, 17), (8, 20), (9, 24), (10, 30)]
|
|
|
|
prices = {i: j for i, j in li}
|
2018-08-29 15:52:02 +08:00
|
|
|
n = 40
|
|
|
|
|
2020-04-15 12:28:20 +08:00
|
|
|
d = {i: count(i, prices) for i in range(n+1)}
|
2018-08-29 15:52:02 +08:00
|
|
|
for i in range(n+1):
|
2020-04-15 12:28:20 +08:00
|
|
|
print(i, d[i])
|