Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'Moses Gitau'
- class FileFormat:
- V = 0
- E = 0
- R = 0
- C = 0
- X = 0
- def __init__(self):
- pass
- class Video:
- ID = 0
- S = 0
- def __init__(self):
- pass
- def __repr__(self):
- return str(self.ID)
- class Endpoint:
- ID = 0
- LD = 0
- K = 0
- cacheServers = []
- def __init__(self):
- pass
- def __repr__(self):
- return str(self.ID)
- class CacheServer:
- ID = 0
- LC = 0
- S = 0
- videoList = set()
- def __init__(self):
- pass
- def __repr__(self):
- return str(self.ID)
- class Requests:
- vID = 0
- eID = 0
- rCount = 0
- def __init__(self):
- pass
- def __repr__(self):
- return str(self.vID)
- file = open("dataset.in", 'r')
- lines = file.readlines()
- f = FileFormat()
- lineContents = lines[0].strip().split(" ")
- f.V = int(lineContents[0])
- f.E = int(lineContents[1])
- f.R = int(lineContents[2])
- f.C = int(lineContents[3])
- f.X = int(lineContents[4])
- # read videos
- videos = lines[1].strip().split(" ")
- videoList = []
- index = 0
- for video in videos:
- v = Video()
- v.ID = int(index)
- v.S = int(video)
- videoList.append(v)
- index += 1
- # read endpoints
- endPointList = []
- index = 2
- for i in range(f.E):
- e = Endpoint()
- endPoint = lines[index].strip().split(" ")
- e.ID = i
- e.LD = int(endPoint[0])
- e.K = int(endPoint[1])
- index += 1
- for j in range(e.K):
- c = CacheServer()
- cacheServer = lines[index].strip().split(" ")
- c.ID = int(cacheServer[0])
- c.LC = int(cacheServer[1])
- e.cacheServers.append(c)
- index += 1
- endPointList.append(e)
- # read requests
- requestsList = []
- for i in range(index, len(lines)):
- r = Requests()
- requests = lines[i].strip().split(" ")
- r.vID = int(requests[0])
- r.eID = int(requests[1])
- r.rCount = int(requests[2])
- endPoint = endPointList[r.eID]
- video = videoList[r.vID]
- if endPoint.K is not 0 and r.rCount is not 0 and video.S < f.X:
- requestsList.append(r)
- requestsList.sort(key=lambda x: x.rCount, reverse=True)
- cacheList = {}
- for request in requestsList:
- endPoint = endPointList[request.eID]
- caches = endPoint.cacheServers
- caches.sort(key=lambda x: x.LC)
- video = videoList[request.vID]
- for cache in caches:
- if f.X - cache.S >= video.S:
- if cacheList.has_key(cache.ID):
- c = cacheList[cache.ID]
- c.videoList.add(video.ID)
- c.S += video.S
- else:
- cache.videoList.add(video.ID)
- cache.S += video.S
- cacheList[cache.ID] = cache
- break
- print(len(cacheList))
- for cache in cacheList.values():
- result = str(cache.ID) + ""
- for video in cache.videoList:
- result = result + " " + str(video)
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement