0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ABC175 C - Walking Takahashi に挑戦した

Posted at

abc175_1.png
abc175_2.png
abc175_3.png

なんかコレ↓の類題じゃね?

有識者は最適解を求められるのだろうが、
私は一個一個場合分けしないと edge case を
網羅できる自信が無かった。

WalkingTakahashi.py
x,k,d = map(int,input().split())
cnt = abs(x // d)
if x == 0:
    if k%2 == 0:
        print(0)
    else:
        print(d)
elif x > 0:
    if abs(k-cnt)%2 == 0:
        if k < cnt:
            print(abs(x-k*d))
        else:
            print(abs(x-cnt*d))
    else:
        if k < cnt:
            print(abs(x-k*d))
        else:
            print(min(abs(x-(cnt+1)*d),abs(x-(cnt-1)*d))) # <= ココ
else:
    if abs(k-cnt)%2 == 0:
        if k < cnt:
            print(abs(x+k*d))
        else:
            print(abs(x+cnt*d))
    else:
        if k < cnt:
            print(abs(x+k*d))
        else:
            print(min(abs(x+(cnt+1)*d),abs(x+(cnt-1)*d)))# <= ココ

手探り感、バリバリ(笑)
コメントにある ココ 、記述にあるように cnt+1 or cnt-1 は、その時の状況に合わせて
最小値を選択する必要がある。ここに気付くまでに WA を 2 回引いた。

もっとスマートに、サクッと出来るようになりたい。
まずは自力で辿り着けてホッとした。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?