사실 algospot을 풀면서 정답을 제출 한 뒤에 저보다 더 낳은 코드를 작성하신 분들의 것들을 보면서 많이 배우고 있는 1인 입니다.
그럴 때 느끼던 생각이..
'아 이런 엘레강스한 코드에 주석이 조금만 잇엇어도.. 이해하기 쉬울텐데' 하는 생각때문에 나도 항상 주석을 달하야지 하지만서도..
디버깅을 하다가 불쑥 정답을 받아버리면 그 후 주석을 단 코드는 길이가 더 길기 때문에..통계란에 보여지는 코드를 수정 할 수 없음이 안타까웠습니다.
그래서 궁금하네요ㅜㅜ 제출한 코드 수정 기능은 운영진은 가능한가요?
그렇다면..magicpower 문제.. 풀어보고 뿌듯해서 주석단걸로 업데이트 부탁드립니다.
~~~ python
returns an arithmetic sequence sum
a + (a-1) + (a-2) ... + a total of 'm' terms.
def seq(a, m):
s = 0
b = a
while m > 0:
s += b
b -= 1
m -= 1
return s
"""
Given a sequence of equal/biggest numbers 'array[i... j]' (i,j inclusive)
Use them 'subtractMe' times in a greedy fashion.
In order to keep 'array' in order, use the rightmost item first.
"""
def subtract(array, i, j, subtractMe):
sum = 0
#Use a multiple of (j - i + 1) times equally.
m = subtractMe / (j - i + 1)
for itr in range(i, j + 1):
#use a sequence sum.
sum += seq(array[itr], m)
array[itr] -= m
#Use the rest with the rightmost item first.
#The rest <= (j - i + 2)
rem = subtractMe % (j - i + 1)
for itr in range(j, j - rem, -1):
sum += array[itr]
array[itr] -= 1
#return i, j, sum where j is adjusted to cover the new range of eqal/biggest numbers.
return i, j - rem, sum
def solve(array, m):
#sort the array in a DESCENDING order
array.sort(reverse = True)
n = len(array)
#assume array[0] is the single biggest element.
i = 0
j = 0
#find initial 'j' where array[i..j] = biggest/equal.
while j < n - 1:
if array[i] != array[j+1]: break
j += 1
#If the entire array is one of a single element,
if j == n - 1:
#calculate the ans.
amount = min(m, (j - i + 1)*(array[j]))
return subtract(array, i, j, amount)[2]
#'k' points to the second biggest element.
k = j + 1
#initial sum.
s = 0
#Until we end up using all 'm' times.
while m > 0:
#At each turn, we can use a minimum of 'm' times or
#the upper area of array[i..j] that's above the second biggest element, times.
amount = min(m, (j - i + 1)*(array[j] - array[k]))
#decrease 'm'
m -= amount
res = subtract(array, i, j, amount)
#update 'j'
j = res[1]
#we need to extend our array[i..j].
if array[j] == array[k]:
j = k
k = j + 1
#we need to decrease our array[i..j]
else:
j, k = res[1], res[1] + 1
#update 'sum'
s += res[2]
#if we reached the 'end' of our array. i.e.
#all items are 'level'. = same height
#look no further.
if j == n - 1:
amount = min(m, (j - i + 1)*(array[j]))
return s + subtract(array, i, j, amount)[2]
return s
for _ in range(input()):
l = raw_input().split()
m = int(l[1])
l = map(int, raw_input().split())
print solve(l, m)
riceluxs1t
사실 algospot을 풀면서 정답을 제출 한 뒤에 저보다 더 낳은 코드를 작성하신 분들의 것들을 보면서 많이 배우고 있는 1인 입니다.
그럴 때 느끼던 생각이..
'아 이런 엘레강스한 코드에 주석이 조금만 잇엇어도.. 이해하기 쉬울텐데' 하는 생각때문에 나도 항상 주석을 달하야지 하지만서도..
디버깅을 하다가 불쑥 정답을 받아버리면 그 후 주석을 단 코드는 길이가 더 길기 때문에..통계란에 보여지는 코드를 수정 할 수 없음이 안타까웠습니다.
그래서 궁금하네요ㅜㅜ 제출한 코드 수정 기능은 운영진은 가능한가요?
그렇다면..magicpower 문제.. 풀어보고 뿌듯해서 주석단걸로 업데이트 부탁드립니다.
~~~ python
returns an arithmetic sequence sum
a + (a-1) + (a-2) ... + a total of 'm' terms.
def seq(a, m):
s = 0
b = a
while m > 0:
s += b
b -= 1
m -= 1
return s
"""
Given a sequence of equal/biggest numbers 'array[i... j]' (i,j inclusive)
Use them 'subtractMe' times in a greedy fashion.
In order to keep 'array' in order, use the rightmost item first.
"""
def subtract(array, i, j, subtractMe):
def solve(array, m):
#sort the array in a DESCENDING order
array.sort(reverse = True)
for _ in range(input()):
l = raw_input().split()
m = int(l[1])
l = map(int, raw_input().split())
print solve(l, m)
11년 전