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

AtCoder ABC402 振り返り(緑コーダーがPythonでABC問題)

Posted at

AtCoder Beginner Contest 402 振り返り

東京海上日動プログラミングコンテスト2025(AtCoder Beginner Contest 402) - AtCoder

この日はunrated参加にしました。C問題まで3問の解答です。

A - CBC

moji.upper() で大文字に変換できるので、元の文字と比較しました。
しかし、単純に moji.isupper() で大文字判定ができるので、そちらを使うほうが楽でしょう。

S = list(input())
answers = []
for i in range(len(S)):
    # S[i].isupper() を使うほうがわかりやすい
    if S[i].upper() == S[i]:
        answers.append(S[i])
print("".join(answers))

B - Restaurant Queue

待ち行列の問題なので、キューを使って処理します。B問題でキューを使うのは珍しい気もします。
条件が厳しくないので、普通に配列(list)を使っても間に合いそうです(未確認)。

from collections import deque

Q = int(input())

queue = deque()
for i in range(Q):
    query = input().split()
    if query[0] == "1":
        # 1のときは末尾に追加
        queue.append(int(query[1]))
    elif query[0] == "2":
        # 2のときは先頭から取り出す
        num = queue.popleft()
        print(num)

C - Dislike Foods

データの持ち方を工夫して解きました。

# foodDishDict 食材 i が使われている 料理
foodDishDict[1] = [1, 3] # 食材1 は 料理 1,3 で使用
foodDishDict[2] = [1, 3, 5] # 食材2 は 料理 1,3,5 で使用
...

# K[] 料理で使用している食材数
K[1] = 2
K[2] = 3
K[3] = 3
...

上のようなデータ構造を作っておきます。食材を克服したとき、以下の処理を行います。

# 食材1 を克服した場合、料理[1, 3] から食材数を-1する
K[1] = 2 → 1
K[3] = 3 → 2

食材数が 0 になった場合、その料理を食べられることになります。

N, M = map(int, input().split())

K = [] # 料理iに使われている食材の数
foodDishDict = defaultdict(list) # 食材がどの料理に使われているか

# 食材データ読み込み
for i in range(M):
    KA = list(map(int, input().split()))
    K.append(KA[0])
    for j in range(1, len(KA)):
        foodDishDict[KA[j]].append(i+1)

# 食材克服データ読み込み
B = list(map(int, input().split()))

caneat = 0 # 食べられる料理の数
for b in B:
    dishlist = foodDishDict[b]
    # 克服した食材を使っている料理(dish)をループ
    for dish in dishlist:
        # 使用食材数を-1
        K[dish-1] -= 1

        # 使用食材数が0になれば、食べられる
        if K[dish-1] == 0:
            caneat += 1
    print(caneat)
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?