[プログラマーlv 2]アーチェリー大会

1340 ワード

解くのに本当に時間がかかりました.
繰返し組合せの完全探索を用いて,スコア差の最大値を更新し,解いた.
...よく覚えなければならない文章です.
ソースコード
from itertools import combinations_with_replacement
def solution(n, info):
    answer = [0]*len(info)
    maximum=0
    info=info[::-1]
    if n>1:
        for indexes in combinations_with_replacement(range(11),n):
            for i in indexes:
                answer[i]+=1
            
            if compare(info,answer)>maximum:
                maximum=compare(info,answer)
                can=answer[:]    
            answer = [0]*len(info)  
        
    else:
        candidate=[0,1,2,3,4,5,6,7,8,9,10]
        for i in candidate:
            answer[i]=1
            
            if compare(info,answer)>maximum:
                maximum=compare(info,answer)
                can=answer[:]
                
            answer[i]=0
        
    if maximum==0:return[-1]
    else:
        return can[::-1]

def compare(info,answer):
    answersum=0
    infosum=0
    for i in range(len(info)):
        if answer[i]==0 and info[i]==0:continue
        if answer[i]>info[i]:
            answersum+=i
        else:
            infosum+=i
    
    if answersum>infosum:

        return answersum-infosum
    else:
        return 0
info[:-1]:反転
組合せwith置換と製品の違い
deepcopy[:]
to be updated