LoginSignup
0
0

More than 3 years have passed since last update.

はじめに

前回
Boot camp for Beginnersをやっていきます。

#37

ARC091-C

考えたこと
操作回数が偶数回なので、縁は必ず終了時に表になります。同様に縁以外の要素は奇数回操作が行われるので、終了時に裏になります。終了時に裏になっている枚数を上の考えを元に式にすると$(n-2)*(m-2)$となります。しかしこれは$n\leq2 ,m\leq2$の場合は成り立ちません。なので、例外($n\leq2 ,m\leq2$)時の処理を分けて書きます。

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

if n == 1 and m == 1:
    print(1)
elif n == 1 or m == 1:
    print(max(max(n,m)-2,0))
else:
    print((n-2)*(m-2))

ARC080-C

考えたこと
4の倍数が全要素の半分以上ならば条件を満たす数列は作れます。ここで考えなければならないのは、2の処理についてです。他の4の倍数でない数と違って2は連続だったら条件を満たします。ですので、nからc2の個数を引いてそこに1足してあげることで2の個数を無視して計算できます。

n = int(input())
a = list(map(int,input().split()))

c1 = 0
c2 = 0
c4 = 0
for i in range(n):
    if a[i] % 4 == 0:
        c4 += 1
    elif a[i] % 2 == 0:
        c2 += 1
    elif a[i] == 1:
        c1 += 1

n -= c2
c2 %= 2
n += c2
f = n // 2

if c4 >= f:
    print('Yes')
    quit()
else:
    print('No')

ABC043-B

考えたこと
forでsを一つずつ拾って文字列に足していくだけ。'B'はスライスで実装しました。

s = input()
ans = ''
for i in range(len(s)):
    if s[i] == 'B':
        ans = ans[:-1]
    elif s[i] == '0':
        ans += '0'
    else:
        ans += '1'
    #print(ans)
print(ans)

ABC131-C

考えたこと
整数の問題だと思います。式の説明

import fractions
a, b, c, d = map(int,input().split())

l = int(fractions.gcd(c,d) * c / fractions.gcd(c,d) * d / fractions.gcd(c,d))
#print(l)
x = b // c - a // c
y = b // d - a // d
z = b // l - a // l
if a % c == 0 or a % d == 0:
    print(int(b-a-(x+y-z)))
else:
    print(int(b-a-(x+y-z))+1)
#print(x,y,z)

ABC109-C

考えたこと
答えは要素間の差の最大公約数になるので、計算するだけ

import fractions
n, y = map(int,input().split())
x = list(map(int,input().split()))

x.append(y)
x.sort()
d = []
for i in range(n):
    d.append(x[i+1]-x[i])
if len(d) !=1:
    ans = fractions.gcd(d[0],d[1])
    for i in range(n-1):
        ans = fractions.gcd(ans,d[i])
    print(ans)
else:
    ans = d[0]
    print(ans)

ABC116-C

考えたこと
全ての花の高さが0の状態から$h$にするのと、$h$から全ての花の高さを0にすることは同じです。
それぞれの$h$に到達していない隣接した花は一緒に水やりを行った方が回数が少なります。また、$h$に到達している花には水やりをしてはいけません。ですので、花が0であるかどうかを調べて一回で水やりできる最大の範囲を計算しています。

n = int(input())
h = list(map(int,input().split()))

ans = 0
for i in range(max(h)):
    c = 0
    for j in range(n):
        if h[j] != 0:
            h[j] -= 1
            c += 1
        else:
            if c != 0:
                ans += 1
                c = 0
        if j == n-1 and c != 0:
            ans += 1
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