Hello,
I was doing this problem called load balancing. Here was my original code:
#Note that a and b need to be less than the max y and x coord and greater than the min y and x coords.
infile = open('balancing.in', 'r')
lines = infile.readlines()
lines.pop(0)
xCoords = []
yCoords = []
coords = []
for line in lines:
line = line.split()
xCoords.append(int(line[0]))
yCoords.append(int(line[1]))
coords.append([int(line[0]), int(line[1])])
minXVal = min(xCoords) + 1
maxXVal = max(xCoords) - 1
minYVal = min(yCoords) + 1
maxYVal = max(yCoords) - 1
minList = []
for yCoord in range(minYVal, maxYVal+1, 2): #We loop through all the x lines
for xCoord in range(minXVal, maxXVal+1, 2): #And the y lines
quadrants = [0,0,0,0] #Smaller smaller = 0, smaller larger = 1, larger smaller = 2, larger larger = 3
for coord in coords:
if coord[0] < xCoord: #Then we just find the corresponding square.
if coord[1] < yCoord:
quadrants[0] += 1
else:
quadrants[1] += 1
else:
if coord[1] < yCoord:
quadrants[2] += 1
else:
quadrants[3] += 1
minList.append(max(quadrants))
infile.close()
outFile = open('balancing.out', 'w')
outFile.write(str(min(minList)))
outFile.close()
But then I got TLEs for the last 4 test cases. So I looked at the solution, and coded it like so:
#Note that a and b need to be less than the max y and x coord and greater than the min y and x coords
infile = open('balancing.in', 'r')
lines = infile.readlines()
lines.pop(0)
xCoords = []
yCoords = []
coords = []
for line in lines:
line = line.split()
xCoords.append(int(line[0]))
yCoords.append(int(line[1]))
coords.append([int(line[0]), int(line[1])])
minXVal = min(xCoords) + 1
maxXVal = max(xCoords) - 1
minYVal = min(yCoords) + 1
maxYVal = max(yCoords) - 1
minList = []
for yCoord in range(maxYVal, minYVal-1, -2): #We loop through all possible x lines
if yCoord - 1 not in yCoords:
continue
for xCoord in range(maxXVal, minXVal+1, -2):
if xCoord - 1 not in xCoords:
continue
quadrants = [0,0,0,0] #Smaller smaller = 0, smaller larger = 1, larger smaller = 2, larger larger = 3
for coord in coords:
if coord[0] < xCoord:
if coord[1] < yCoord:
quadrants[0] += 1
else:
quadrants[1] += 1
else:
if coord[1] < yCoord:
quadrants[2] += 1
else:
quadrants[3] += 1
minList.append(max(quadrants))
infile.close()
outFile = open('balancing.out', 'w')
outFile.write(str(min(minList)))
outFile.close()
.
But I still got TLEs for the last 3 cases. Am I doing something wrong?
Thanks,
~Zanderman