Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rawFile = open("me_at_the_zoo.in", "r")
- fileCon = [lines.replace("\n","").split(" ") for lines in rawFile]
- rawFile.close()
- oldper = 100
- V = int(fileCon[0][0])
- E = int(fileCon[0][1])
- R = int(fileCon[0][2])
- C = int(fileCon[0][3])
- X = int(fileCon[0][4])
- Vs = [int(x) for x in fileCon[1]]
- Lds = [-1 for i in range(E)]
- Cs = [[-1 for i in range(E)] for j in range(C)]
- Ks = [0 for i in range(E)]
- RemSpace = [X for i in range(C)]
- cacheContents = [[] for i in range(C)]
- start = 2
- for i in range(E):
- numcaches = int(fileCon[start][1])
- Ks[i]=numcaches
- Lds[i] = (int(fileCon[start][0]))
- for x in range(numcaches):
- start += 1
- Cs[int(fileCon[start][0])][i] = int(fileCon[start][1])
- start += 1
- Rs = [[0 for x in range(E)] for y in range(V)]
- counter = 0
- for k in range(start,len(fileCon)):
- temp4 = [int(i) for i in fileCon[k]]
- Rs[temp4[0]][temp4[1]] = temp4[2]
- counter += 1
- def savefun(v,c):
- total = 0
- totalreqs=0
- for e in range(E):
- totalreqs+=Rs[v][e]
- if (Cs[c][e] != -1):
- total += (Lds[e]-Cs[c][e])*Rs[v][e]
- if(totalreqs==0):
- return 0;
- return total/totalreqs
- savings = [[savefun(v,c) for v in range(V)] for c in range(C)]
- def numLeft():
- temp=0
- for v in range(V):
- for c in range(C):
- if(savings[c][v])!=0:
- temp+=1
- return temp
- maxitems = numLeft()
- def popMax():
- tempCount = 0;
- maxij = (0,0);
- maxval = 0;
- for v in range(V):
- for c in range(C):
- if savings[c][v]!=0:
- tempCount+=1
- if (savings[c][v] > maxval):
- maxval = savings[c][v]
- maxik = (v,c)
- if (maxval != 0):
- maxv= maxik[0]
- maxc= maxik[1]
- if (RemSpace[maxc]-Vs[maxv]>=0):
- RemSpace[maxc]-=Vs[maxv]
- cacheContents[maxc].append(maxv)
- #print("Added video " + str(maxv) + " to cache " + str(maxc))
- savings[maxc][maxv]=0
- return True
- else:
- return False
- while(popMax()):
- pass;
- usedcaches = 0
- for c in range(C):
- if cacheContents[c] != []:
- usedcaches+=1
- fileOut = open("output.txt","w")
- print (str(usedcaches), file=fileOut)
- for c in range(C):
- if(cacheContents[c] != []):
- print(str(c) + " " + " ".join([str(x) for x in cacheContents[c]]), file=fileOut)
- fileOut.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement