Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. rawFile = open("me_at_the_zoo.in", "r")
  2. fileCon = [lines.replace("\n","").split(" ") for lines in rawFile]
  3. rawFile.close()
  4.  
  5. oldper = 100
  6.  
  7. V = int(fileCon[0][0])
  8. E = int(fileCon[0][1])
  9. R = int(fileCon[0][2])
  10. C = int(fileCon[0][3])
  11. X = int(fileCon[0][4])
  12.  
  13. Vs = [int(x) for x in fileCon[1]]
  14. Lds = [-1 for i in range(E)]
  15. Cs = [[-1 for i in range(E)] for j in range(C)]
  16. Ks = [0 for i in range(E)]
  17. RemSpace = [X for i in range(C)]
  18.  
  19. cacheContents = [[] for i in range(C)]
  20.  
  21.  
  22.  
  23.  
  24. start = 2
  25. for i in range(E):
  26.     numcaches = int(fileCon[start][1])
  27.     Ks[i]=numcaches
  28.     Lds[i] = (int(fileCon[start][0]))
  29.     for x in range(numcaches):
  30.         start += 1
  31.         Cs[int(fileCon[start][0])][i] = int(fileCon[start][1])
  32.     start += 1
  33.    
  34. Rs = [[0 for x in range(E)] for y in range(V)]
  35. counter = 0
  36. for k in range(start,len(fileCon)):
  37.     temp4 = [int(i) for i in fileCon[k]]
  38.     Rs[temp4[0]][temp4[1]] = temp4[2]
  39.     counter += 1
  40.  
  41.  
  42. def savefun(v,c):
  43.     total = 0
  44.     totalreqs=0
  45.     for e in range(E):
  46.         totalreqs+=Rs[v][e]
  47.         if (Cs[c][e] != -1):
  48.             total += (Lds[e]-Cs[c][e])*Rs[v][e]
  49.     if(totalreqs==0):
  50.         return 0;
  51.     return total/totalreqs
  52. savings = [[savefun(v,c) for v in range(V)] for c in range(C)]
  53.  
  54.  
  55. def numLeft():
  56.     temp=0
  57.     for v in range(V):
  58.         for c in range(C):
  59.             if(savings[c][v])!=0:
  60.                 temp+=1
  61.     return temp
  62.  
  63. maxitems = numLeft()
  64.  
  65. def popMax():
  66.     tempCount = 0;
  67.     maxij = (0,0);
  68.     maxval = 0;
  69.     for v in range(V):
  70.         for c in range(C):
  71.             if savings[c][v]!=0:
  72.                 tempCount+=1
  73.             if (savings[c][v] > maxval):
  74.                 maxval = savings[c][v]
  75.                 maxik = (v,c)
  76.    
  77.     if (maxval != 0):
  78.         maxv= maxik[0]
  79.         maxc= maxik[1]
  80.         if (RemSpace[maxc]-Vs[maxv]>=0):
  81.             RemSpace[maxc]-=Vs[maxv]
  82.             cacheContents[maxc].append(maxv)
  83.             #print("Added video " + str(maxv) + " to cache " + str(maxc))
  84.            
  85.         savings[maxc][maxv]=0
  86.        
  87.         return True
  88.     else:
  89.         return False
  90.  
  91. while(popMax()):
  92.     pass;
  93.  
  94. usedcaches = 0
  95. for c in range(C):
  96.     if cacheContents[c] != []:
  97.         usedcaches+=1
  98. fileOut = open("output.txt","w")
  99. print (str(usedcaches), file=fileOut)
  100. for c in range(C):
  101.     if(cacheContents[c] != []):
  102.         print(str(c) + " " + " ".join([str(x) for x in cacheContents[c]]), file=fileOut)
  103. fileOut.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement