-
-
Save neetsdkasu/00f81c079e01e5f7da1fefad2a8ee127 to your computer and use it in GitHub Desktop.
MM26 Python Solution (check time every times)
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, time | |
class Permute: | |
limit = 29.8 | |
def findOrder(self, w): | |
time0 = time.time() + self.limit | |
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 1: | |
time1 = time.time() | |
if time1 > time0: | |
break | |
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