Open6
競プロ「初中級者が解くべき過去問精選 100 問」 全探索:工夫して通り数を減らす全列挙
05. AtCoder Beginner Contest 095 C - Half and Half
main.py
#!/usr/bin/env python3
import sys
def main():
A, B, C, X, Y = [int(x) for x in sys.stdin.readline().rstrip().split()]
max_xy = max(X, Y)
result = int(A*X + B*Y)
for i in range(2, 2*max_xy+2, 2):
x = int(X - i/2)
y = int(Y - i/2)
r = int(C*i)
if x > 0: r += A*x
if y > 0: r += B*y
if result > r:
result = r
print(result)
if __name__ == '__main__':
main()
実行結果
06. 三井住友信託銀行プログラミングコンテスト 2019 D - Lucky PIN
main.py
#!/usr/bin/env python3
import sys
def main():
N = int(sys.stdin.readline().rstrip())
S = sys.stdin.readline().rstrip()
result = set()
for i in range(1000):
s3 = '{:03}'.format(i)
j = 0
s = S
for ss in s3:
if s.find(ss) == -1:
break
else:
s = s[s.find(ss)+1:]
j += 1
if j == 3:
result.add(s3)
print(len(result))
if __name__ == '__main__':
main()
実行結果
解説
もっとスマートに書きたい……
てか、眠い……
07. JOI 2007 本選 3 - 最古の遺跡
main.py
#!/usr/bin/env python3
import sys
def main():
N = int(sys.stdin.readline().rstrip())
XY = [tuple(map(int, sys.stdin.readline().rstrip().split())) for _ in range(N)]
set_xy = set(XY)
result = 0
for i, (x1, y1) in enumerate(XY):
for (x2, y2) in XY[i+1:]:
vector_x, vector_y = (x2-x1, y2-y1)
xy3 = (x2 - vector_y, y2 + vector_x)
xy4 = (x1 - vector_y, y1 + vector_x)
if (xy3 in set_xy) and (xy4 in set_xy):
result = max(result, vector_x**2 + vector_y**2)
print(result)
if __name__ == '__main__':
main()
実行結果
解説
08. Square869120Contest #6 B - AtCoder Markets
main.py
#!/usr/bin/env python3
import sys
import statistics
def main():
N = int(sys.stdin.readline().rstrip())
read = sys.stdin.read
AB = [int(x) for x in read().rstrip().split()]
A = AB[0::2]
B = AB[1::2]
start = int(statistics.median(A))
goal = int(statistics.median(B))
result = 0
for a, b in zip(A, B):
result += abs(start - a) + abs(a - b) + abs(goal - b)
print(result)
if __name__ == '__main__':
main()
実行結果
解説
09. JOI 2008 予選 4 - 星座探し
main.py
#!/usr/bin/env python3
import sys
def main():
M = int(sys.stdin.readline().rstrip())
M_XY = [tuple(map(int, sys.stdin.readline().rstrip().split())) for _ in range(M)]
N = int(sys.stdin.readline().rstrip())
N_XY = [tuple(map(int, sys.stdin.readline().rstrip().split())) for _ in range(N)]
reference = set()
m_x0 = M_XY[0][0]
m_y0 = M_XY[0][1]
for (x1, y1) in M_XY:
v = (m_x0-x1, m_y0-y1)
reference.add(v)
vectors = set()
result_x, result_y = 0, 0
for (x1, y1) in N_XY:
for (x2, y2) in N_XY:
v = (x1-x2, y1-y2)
vectors.add(v)
if reference <= vectors:
result_x, result_y = x1 - m_x0, y1 - m_y0
break
vectors.clear()
print("{} {}".format(result_x, result_y))
if __name__ == '__main__':
main()