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 1 year has passed since last update.

AtCoder初心者 DailyTrainingメモ③

Last updated at Posted at 2023-10-22

ABC269 B-問題

269B.png

ポイント
・図形の境界値をset()で取得する
・空白をはさんで出力 print(A,B,sep=" ")

269B.py
# 入力
..........
..........
..........
..........
...######.
...######.
...######.
...######.
..........
..........

from itertools import permutations
import math,sys,datetime,random,glob,os,re

row = set() # 行のset
col = set() # 列のset

# 入力
for i in range(10):
    S = input()             # 1行読み込む
    for j in range(10):     # 1文字ずつ判定する
        if S[j] == '#':     # 「#」の位置を記憶させる
            row.add(i+1)    # 行のsetに i+1 を格納
            col.add(j+1)    # 列のsetに j+1 を格納

# それぞれの境界値を取得する
A = min(row)   # 行の最小値
B = max(row)   # 行の最大値
C = min(col)   # 列の最小値
D = max(col)   # 列の最大値

print(A,B,sep=" ")  # 空白をはさんで出力
print(C,D,sep=" ")

ABC235 C-問題

235C.png

ポイント
・連想配列に格納する
・ループでクエリ(問い)に回答する

235C.py
from itertools import permutations
from collections import defaultdict
import math,sys,datetime,random,glob,os,re

# 入力
6 8
1 1 2 3 1 2
1 1
1 2
1 3
1 4
2 1
2 2
2 3
4 1

N,Q = map(int,input().split())
A = list(map(int, input().split()))

# 連想配列のリストクラスへ格納する
M = defaultdict(list)
for i in range(N):
    M[A[i]].append(i + 1)  # 出現順(インデックス)を記憶させたいので [i+1]

# M: defaultdict(<class 'list'>, {1: [1, 2, 5], 2: [3, 6], 3: [4]})

for _ in range(Q):
    X,K = map(int,input().split())
    if K <= len(M[X]):
        print(M[X][K-1])    # インデックスなので [K-1]
    else:
        print(-1)

# 出力
1
2
5
-1
3
6
-1
-1

ABC272 C-問題

問題文
長さ
N の非負整数列 A=(A1,A2,…,AN) が与えられます。
A の異なる 2 要素の和として表せる値の中に偶数が存在するか判定し、存在する場合その最大値を求めてください。

ポイント
計算量を考えて偶数グループと奇数グループに分ける。和が偶数になる場合は、偶数同士奇数同士の組み合わせのみ

272C.py
入力例
3
2 3 4

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

# 計算量を考えて偶数グループと奇数グループに分ける
# 和が偶数になる場合は、偶数同士奇数同士の組み合わせのみ

add = []
even = []

ans = -1
for i in range(N):
    if A[i] % 2 == 0:
        add.append(A[i])
    else:
        even.append(A[i])

add.sort(reverse=True) # 降順に並べ替え
even.sort(reverse=True)

addsum = 0
evensum = 0

# 偶数グループ、奇数グループのTOP2 の和を算出
if len(add) >= 2:
    addsum = add[0]+add[1]
    ans = max(ans,addsum)

if len(even) >= 2:
    evensum = even[0]+even[1]
    ans = max(ans,evensum)

print(ans)
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?