Created
November 15, 2015 14:24
-
-
Save tjkendev/235d693ddee3fb9cb2b9 to your computer and use it in GitHub Desktop.
Code Festival 2015 朝プロMiddle - submit
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
n,k,m,r = map(int, raw_input().split()) | |
s = sorted((input() for i in xrange(n-1)), reverse=True) | |
sk = sum(s[:k]) | |
sk1 = sum(s[:k-1]) | |
if sk>=r*k: | |
print "0" | |
else: | |
p = r*k-sk1 | |
print p if 0<=p<=m else "-1" |
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
n = input() | |
s = raw_input() | |
def dfs(i, j, ci, cj): | |
if dp[ci][cj] != -1: | |
return dp[ci][cj] | |
if i+ci >= j or j+cj >= n: | |
return max(0, j-(i+ci)) + max(0, n-(j+cj)) | |
if s[i+ci] == s[j+cj]: | |
dp[ci][cj] = dfs(i, j, ci+1, cj+1) | |
else: | |
dp[ci][cj] = min(dfs(i, j, ci+1, cj), dfs(i, j, ci, cj+1)) + 1 | |
return dp[ci][cj] | |
ans = n | |
for i in xrange(n): | |
for j in xrange(i+1, n): | |
if s[i]==s[j]: | |
dp = [[-1]*n for k in xrange(n)] | |
ans = min(ans, dfs(i, j, 1, 1) + i) | |
print ans |
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
from collections import deque | |
n = input() | |
deq = deque() | |
deq.extend(map(int, raw_input().split())) | |
ans = 0 | |
while len(deq)>1: | |
l = deq[-1]; r = deq[0]; | |
lb = deq[-2]; rb = deq[1] | |
if 2*l+lb >= 2*r+rb: | |
ans += 2*r+rb+1 | |
deq.popleft() | |
deq.popleft() | |
re = deq.popleft() | |
deq.appendleft(r+rb+2+re) | |
else: | |
ans += 2*l+lb+1 | |
deq.pop() | |
deq.pop() | |
re = deq.pop() | |
deq.append(l+lb+2+re) | |
print ans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment