LoginSignup
0
0

More than 3 years have passed since last update.

競技プログラミング 日記 python 20201220

Posted at

A - Brick

n, w = map(int, input().split())

print(n // w)

B - Blocks on Grid

全てのマスの中で最も少ない数に合わせる。10の4乗程度のオーダーなので愚直にループを回せば良さそう

h, w = map(int, input().split())

blocks = []

for i in range(h):
    blocks += list(map(int, input().split()))

min_block = min(blocks)

ans = 0

for a in blocks:
    ans += a - min_block

print(ans)

C - Unlucky 7

1からNまでの数のうち、10進法でも8進法でも7を含まない物はいくつ?

Nは10の5乗以下なので、N以下のすべての数に対して確認すれば良さそうかな

n = int(input())

ans = 0

for a in range(1, n+1):
    ten = str(a)
    eight = format(a, 'o')
    if "7" not in ten and "7" not in eight:
        ans += 1
print(ans)

D - Sum of difference

Nこの数字に足して、すべての差の合計を求める。combinationを求めてループすればいい?
それだと計算量がオーバーする。大きさ順に事前にソートしておくと、絶対値の計算が要らなそう

n = int(input())
numbers = sorted(list(map(int, input().split())))

ans = 0
other_sum = sum(numbers)
other_len = len(numbers)

for i, a in enumerate(numbers):
    other_sum -= a
    other_len -= 1
    ans += (other_sum) - a * (other_len)

print(ans)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0