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?

[ABC432] ABC 432(Atcoder Beginner Contest)のA~C(A,B,C)問題をPythonで解説(復習)

Posted at

[ABC432] ABC 432(Atcoder Beginner Contest)のA~C(A,B,C)問題をPythonで解説(復習)

A問題

  • 入力を降順ソートしてくっ付ければ良さそう。
A.py
"""
<方針>
- 入力を降順ソートしてくっ付ければ良さそう。
"""
# 入力
ABC = list(map(str, input().split()))

# 降順ソート
ABC.sort(reverse=True)

# 答え
ans = "".join(ABC)

# 出力
print(ans)

B問題

  • ソートするけど、0が手前に来ないように右にあるやつを一番左にシフトする。
B.py
"""
<方針>
- ソートするけど、0が手前に来ないように右にあるやつを一番左にシフトする。
"""
# 入力
X = list(input())

# ソート
X.sort()

# 0じゃないやつを見つける
for i, x in enumerate(X):
  if(x != "0"):
    le = X[:i]
    mi = [X[i]]
    ri = X[i+1:]
    # 入れ替え
    ans = mi + le + ri
    break

# 出力
print("".join(ans))

C問題

方針

  • O(N) になるように、線形で導ける方法を考える。
  • 単純化のため、A をソートしてみる。
  • 方程式を解くと、それぞれが何個食べれば良いかが分かる。
    • めんどくさいので、公式解説をどうぞ。

前提

  • C問題あたりで,TLEになる人は,制約条件を見る癖をつけよう.
  • AB問題では,基本的に制約条件を見ずにやっても解ける.
  • しかし,C問題以降では,制約条件を見ないと必ずTLEすると思っても良い.
  • 詳しい話は私の352回の記事C問題の解説に記したので,是非参照してほしい.
C.py
"""
<方針>
- `O(N)` になるように、線形で導ける方法を考える。
- 単純化のため、`A` をソートしてみる。
- 方程式を解くと、それぞれが何個食べれば良いかが分かる。
  - めんどくさいので、公式解説をどうぞ。
"""
N, X, Y = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

ans = 0
for a in A:
  di, mo = divmod((a-A[0])*X, Y-X)
  if(mo!=0):
    print(-1)
    exit()
  
  ad = A[0]-di
  if(ad<0):
    print(-1)
    exit()
  
  ans += ad

print(ans)

補足

関係するリンク(参考文献など)

筆者について

その他

  • 間違いを含んでいる可能性があります.
  • 方針と言いつつ,方針ではない感想などを書いている可能性があります.
  • A問題から解説がだんだん省略されます.
  • コードに書かれている解説の文言と,その上に書いてある解説の文言を変えている場合があります.

最後に一言

  • なこんさん、応援してます。
  • というか、ずっと元気もらってます。
  • ずっとありがとうございます。
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?