|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +ceny = [ 1, 5, 8, 9,10,16,17,20,24,26] |
| 4 | +dlug = [ 1, 2, 3, 4, 5, 6, 7, 8, 9,10] |
| 5 | +dl = (int)(input()) |
| 6 | +def cutToMoney(cut): |
| 7 | + money =0 |
| 8 | + for c in cut: |
| 9 | + money+=ceny[(dlug.index(c))] |
| 10 | + return money; |
| 11 | + |
| 12 | +def getKey(key): |
| 13 | + return (float)(key[0]/key[1]) |
| 14 | +def greedy(dl): |
| 15 | + global ceny |
| 16 | + global dlug |
| 17 | + greedyTab =[]; |
| 18 | + for i in range(len(ceny)): |
| 19 | + greedyTab.append((ceny[i],dlug[i])); |
| 20 | + print (greedyTab) |
| 21 | + greedyTab=(sorted(greedyTab, key=getKey,reverse=True)) |
| 22 | + print (greedyTab) |
| 23 | + cut = [] |
| 24 | + while(dl >0): |
| 25 | + for cd in greedyTab: |
| 26 | + if(cd[1]<=dl):#wybieramy lokalnie najlepszą cenę |
| 27 | + cut.append(cd[1]) |
| 28 | + dl-=cd[1]#zmniejszamy pret |
| 29 | + break;#przechodzimy do kolejnego punktu cięcia |
| 30 | + return cut |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +backPackTab =[ |
| 35 | + (0,0,[]), |
| 36 | + (1,1,[1]), |
| 37 | + (2,5,[2]), |
| 38 | + (3,8,[3])] |
| 39 | +backPackTabCount = 3; |
| 40 | +def backpack(dl): |
| 41 | + global ceny |
| 42 | + global dlug |
| 43 | + global backPackTab |
| 44 | + global backPackTabCount |
| 45 | + if(dl<=backPackTabCount): |
| 46 | + return backPackTab[dl]; |
| 47 | + |
| 48 | + for i in range(len(backPackTab),dl+1): |
| 49 | + for t_dlug in dlug: |
| 50 | + if(t_dlug> i): |
| 51 | + break; |
| 52 | + |
| 53 | + if(i <= backPackTabCount): |
| 54 | + cached =backPackTab[i] |
| 55 | + else: |
| 56 | + cached = None |
| 57 | + new = (i,backPackTab[i-t_dlug][1]+ceny[t_dlug-1],backPackTab[i-t_dlug][2]+[t_dlug]) |
| 58 | + |
| 59 | + if(cached != None): |
| 60 | + if(new[1] >= cached[1]): |
| 61 | + backPackTab[i]=new; |
| 62 | + else: |
| 63 | + backPackTab.append(new) |
| 64 | + backPackTabCount=new[0] |
| 65 | + |
| 66 | + return backPackTab[dl] |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +cut =greedy(dl) |
| 71 | +print(cut ,cutToMoney(cut)) |
| 72 | +print(backPackTab) |
| 73 | +cut = backpack(dl)[2] |
| 74 | +print(backPackTab) |
| 75 | +print(cut ,cutToMoney(cut)) |
0 commit comments