Skip to content

Commit

Permalink
Create answer_A49_greedy2_40978pts.py
Browse files Browse the repository at this point in the history
  • Loading branch information
E869120 authored Oct 1, 2022
1 parent 66d6869 commit 39c30ab
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions codes/python/chap07/answer_A49_greedy2_40978pts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
################################################
# 本の 272 ページ後半の評価関数を用いた実装です
################################################

# 入力
T = int(input())
P = [ None ] * T
Q = [ None ] * T
R = [ None ] * T
for i in range(T):
P[i], Q[i], R[i] = map(int, input().split())
P[i] -= 1 # 0 始まりに直す
Q[i] -= 1
R[i] -= 1

# 配列 A の初期化
A = [ 0 ] * 20

# 貪欲法
for i in range(T):
# パターン A の場合のスコアを求める
ScoreA = 0
PatA = [ 0 ] * 20
for j in range(20):
PatA[j] = A[j]
PatA[P[i]] += 1
PatA[Q[i]] += 1
PatA[R[i]] += 1
for j in range(20):
ScoreA += abs(PatA[j])

# パターン B の場合のスコアを求める
ScoreB = 0
PatB = [ 0 ] * 20
for j in range(20):
PatB[j] = A[j]
PatB[P[i]] -= 1
PatB[Q[i]] -= 1
PatB[R[i]] -= 1
for j in range(20):
ScoreB += abs(PatB[j])

# スコアの小さい方を採用
if ScoreA <= ScoreB:
print("A")
CurrentScore = ScoreA
for j in range(20):
A[j] = PatA[j]
else:
print("B")
CurrentScore = ScoreB
for j in range(20):
A[j] = PatB[j]

0 comments on commit 39c30ab

Please sign in to comment.