-
-
Save neetsdkasu/dc4c9d90e487557747db22ec3c7f9051 to your computer and use it in GitHub Desktop.
MM26 Python Solution (140000 cycles)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math, sys | |
class Permute: | |
limit = 140000 | |
def findOrder(self, w): | |
n = int(math.sqrt(len(w))) | |
ret = range(n) | |
sc = 0.0 | |
for i in xrange(n - 1): | |
rn = ret[i] * n | |
for r in ret[i + 1:]: | |
sc += w[rn + r] | |
lp, a, b = 0, 0, 1 | |
while lp < self.limit: | |
lp += 1 | |
ra, rb = ret[a], ret[b] | |
ran, rbn = ra * n, rb * n | |
dsc = w[rbn + ra] - w[ran + rb] | |
for r in ret[a + 1:b]: | |
rn = r * n | |
dsc += w[rbn + r] + w[rn + ra] - w[ran + r] - w[rn + rb] | |
if dsc > 0.0: | |
ret[a], ret[b] = rb, ra | |
sc += dsc | |
b += 1 | |
if b >= n: | |
a += 1 | |
if a >= n - 1: | |
a = 0 | |
b = a + 1 | |
dv = n * math.sqrt(n * math.log(n) - n) / 100.0 | |
sc /= dv | |
sys.stderr.write("score={}\n".format(sc)) | |
sys.stderr.write("loop={}\n".format(lp)) | |
return tuple(ret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment