LoginSignup
1
0

More than 1 year has passed since last update.

AtCoder Beginner Contest 297

Last updated at Posted at 2023-04-09

AtCoder Beginner Contest 297をpythonで解いてみた

はじめに

今回はA,B,C,Dの4完で入茶しました!
時間あれば入茶の記事かきまふ。

A問題 A - Double Click

a.py
def find_first_double_click(N, D, click_times):
    for i in range(N - 1):
        if click_times[i + 1] - click_times[i] <= D:
            return click_times[i + 1]
    return -1
 
N, D = map(int, input().split())
click_times = list(map(int, input().split()))
 
result = find_first_double_click(N, D, click_times)
 
print(result)

A問題で解説することないので飛ばします、、、

B問題 B - chess960

b.py
def is_chess(s):
    b_positions = [i for i, c in enumerate(s) if c == 'B']
    if len(b_positions) != 2 or b_positions[0] % 2 == b_positions[1] % 2:
        return "No"
 
    k_position = s.index('K')
    r_positions = [i for i, c in enumerate(s) if c == 'R']
    if len(r_positions) != 2 or not (r_positions[0] < k_position < r_positions[1]):
        return "No"
 
    return "Yes"
 
S = input().strip()
 
result = is_chess(S)
 
print(result)

今見返すとこんなことしないでも愚直にやってよかったと反省してます、、。
これも解説することないので飛ばします、、

C問題 C - PC on the Table

c.py
H,W = map(int,input().split())
mazes = [list(input()) for _ in range(H)]
for h in range(H):
    for w in range(W-1):
        if mazes[h][w] == "T" and mazes[h][w+1] == "T":
            mazes[h][w],mazes[h][w+1] = "P","C"
for maze in mazes:
    print("".join(maze))

問題文で操作回数を最大化と言ってますが、前から順にやれば大丈夫です。

D問題 D - Count Subtractions

d.py
def solve(a, b):
    count = 0
    while b:
        count += a // b
        a, b = b, a % b
        # print(f"a = {a} b = {b}")
    return count - 1
 
A, B = map(int, input().split())
 
count = solve(A, B)
 
print(count)

この問題は愚直にやったらTLEになりました。。。。なので入力例1のA=3,B=8のとき
A=3,B=(8-3)=5
A=3,B=(5-3)=2
A=(3-2)=1,B=2
A=1,B=(2-1)=1
の4回とやるよりも
8=3*2+2なので
カウントに2を足す。そして
A=3,B=(8-3x2)=2とやるのかなって考えました。けどよくよく考えたら
A=3,B=(8%3)=2というふうにあまり(A%B(A>B))になると言うことがわかり、このやり方なら
間に合うのでは?と思い実装しました。やることは単純なので苦労することはなかったです。
見事ACでした。

最後に

D問題まで24分で解けたのでE問題も解きたかったのですが、手も足も出ず、、
G問題はサンプルのテストケースはACだったのですがWAが11個もでて萎えました、、
E問題も解けるように精進していきまふ。

1
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
1
0