Created
March 27, 2016 04:05
-
-
Save cocodrips/db27c22b378fac15dd9d to your computer and use it in GitHub Desktop.
TCO 2016 Round1 Med
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
import math,string,itertools,fractions,heapq,collections,re,array,bisect | |
class EllysSocks: | |
def getDifference(self, S, P): | |
INF = 100000000000 | |
S = list(S) | |
S.append(0) | |
S.sort() | |
N = len(S) | |
PAIR = len(S) / 2 | |
dp = [[INF for _ in xrange(PAIR + 1)] for _ in xrange(N)] | |
for i in xrange(N): | |
dp[i][0] = -1 | |
for i in xrange(2, N): | |
for pair in xrange(PAIR): | |
# Select | |
dp[i][pair + 1] = min(dp[i][pair + 1], max(dp[i - 2][pair], S[i] - S[i - 1])) | |
# Unselect | |
dp[i][pair] = min(dp[i][pair], dp[i - 1][pair]) | |
return dp[len(S) - 1][P] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment