2
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?

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

Posted at

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

今回は旅行でコンテストに参加できなかったため、終わってから解きました。C問題の考え方が即座に浮かぶにはどうすればいいのだろう、、、

A問題

終了後考えた最適な回答

X,C = map(int,input().split())

# 常に引き出し額+手数料がX以下である必要がある

for i in range(0,X,1000):
    total = i + (i//1000)*C
    if X-C < 1000:
        print(0)
        exit(0)
    if total <= X:
        result = i

print(result)

B問題

終了後考えた最適な回答

N = int(input())
L = list(map(int,input().split()))

x = 0
y = 0
for i in range(N):
    if L[i] == 1:
        x = i
        break

for i in range(N-1,-1,-1):
    if L[i] == 1:
        y = i
        break

print(y-x)

C問題

終了後考えた最適な回答

N,R = map(int,input().split())
L = list(map(int,input().split()))

# 閉操作の後に開操作を行うことはない
# 閉操作の回数=開く操作の回数+始めに開いていた鍵の個数の和
if(L.count(0) == 0):
  # 何もしなくていい
  print(0)
  exit()

x = 0
y = 0
for i in range(N):
    if L[i] == 0:
        x = i
        break

for i in reversed(range(N)):
    if L[i] == 0:
        y = i
        break

mi = min(x,R)

ma = max(y+1,R)

target = L[mi:ma]

ans = target.count(1) + len(target)

print(ans)

次に向けてやること

・B、C問題の問題演習

感想

全体的に難易度高め?相性悪め?の問題だった、特にC問題は解法が全然浮かばず、解説見てなるほどーってなったがどうやって思いつくのだろう。一応これはgreedy法らしい

補足

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

今回のコンテスト

回答の正当性は保証できません。ベストエフォート方式です。

2
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
2
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?