0
0

More than 3 years have passed since last update.

Atcoder ABC161 A-EをPythonで

Last updated at Posted at 2020-04-13

A ABC Swap

Swapを2回行った結果は$(a,b,c) => (c,a,b)$なので、それを出力する。

abc161a.py
a,b,c=map(int,input().split())

print(c,a,b)

B Popular Vote

問題文の通りに実装するだけ・・と思ったら以下のコードでWAが出た。

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

l=list(map(int,input().split()))
s=sum(l)
ans=0
t=s/(4*m)
for i in l:
    if i>t:
        ans+=1
if ans>=m: print("Yes")
else: print("No")

判定条件をi>tからi>=math.ceil(t)に変えたら通った。math.ceil(t)>t>math.floor(t)のはずなので結果は変わりないはずなんだけど・・未だに謎。
(4/13/2020追記:コメントいただいた通り、t=iのケースが抜けてました。)

abc161b.py
import math
n,m=map(int,input().split())

l=list(map(int,input().split()))
s=sum(l)
ans=0
t=s/(4*m)
for i in l:
    if i>=math.ceil(t):
        ans+=1
if ans>=m:
    print("Yes")
else:
    print("No")

C Replacing Integer

操作:$x$を$x$と$K$の差の絶対値で置き換える、を繰り返した場合最小になる値は$Nmod(k)$または$|N mod(k)-K|$のどちらか小さいほうになるのでそれを出力する。例えば$N=7, K=3$の時は
$Nmod(K)=1, |Nmod(k)-K|=|-2|=2$だが、
$K=4$だと$Nmod(K)=3$, $|Nmod(k)-K|=|-1|=1$となる。

abc161c.py
n,k=map(int,input().split())

a=n%k
b=abs(n%k-k)

print(min(a,b))

D Lunlun number

初めに1-9が格納された配列を用意する。ルンルン数はこの各要素に$(-1,0,1)$を足した値を後ろに加えた値になる。例外として、$9+1=10$になるのでこれを除く。配列の要素数をカウントして$i>10^5$になったら打ち止め。

abc161d.py
k=int(input())

q=["1","2","3","4","5","6","7","8","9"]
x=9
for i in q:
    if x>100000:
        break
    x+=1
    a=i[-1]
    for j in ([-1,0,1]):
        bb=int(a)+j
        if bb<10 and bb>=0:
            q.append(i+str(bb))


print(q[k-1])

E Yutori

左端から〇を数え上げていった場合、そのステップ数は「この日までに働ける最大の日数」になる。
右側から〇を数え上げていった場合、そのステップ数は「この日から働ける最大の日数」になる。

この2つのリストを使って、$i$日目に働かない場合の最大稼働日数を算出するには、
左端からのリストの$i-1$日目のステップと右端からのリストの$i+1$日目のステップの和を求めればいい。
この和が$K$日に満たない時、その日は必ず働かなければいけない日になる。

35-38行目はもっとスマートなやり方があるような気がします。

abc161e.py
n,k,c=map(int,input().split())
s=input()

available=[]
for i in range(n):
    if s[i]=="o":
        available.append(i)

#左側から〇を数える
left=[0]*n
rest=0
cnt=0

for i in range(n):
    if s[i]=="o" and i>=rest:
        cnt+=1
        left[i]=cnt
        rest=i+c+1
    else:
        left[i]=cnt

#右側から〇を数える
right=[0]*n
rest=n-1
cnt=0

for i in range(n-1,-1,-1):
    if s[i]=="o" and i<=rest:
        cnt+=1
        right[i]=cnt
        rest=i-c-1
    else:
        right[i]=cnt

#-1日目、n+1日目がエラーにならないように0を足す        
for i in ([left,right]):
    i.insert(0,0)
    i.append(0)

ans=[]

for i in range(1,n+1):
    a=left[i-1]+right[i+1]
    if a<k:
        ans.append(i)

for j in ans:
    print(j)
0
0
2

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