atcoder abc_124 を解いてみた(python3)
-A. [AC] Buttons
問題ページ【A - Buttons】
私のコンテスト中の解法
# -*- coding: utf-8 -*- A,B = map(int,input().split() ) sum = 0 for i in range(2): if A >= B : sum = sum + A A = A - 1 else: sum = sum + B B = B - 1 print(sum)
実行時間:17 ms メモリ:2940 KB
forループを二回まわしてAとBを比較する。
大きい方を合計に足していく。
chokudaiさんの解説の解法
# -*- coding: utf-8 -*- A,B = map(int,input().split()) sum = 0 sum = max(sum, A + A -1) sum = max(sum, A + B) sum = max(sum, B + B -1) print(sum)
実行時間:17 ms メモリ:2940 KB
ボタンは二回押すので、AとBの組み合わせは3つだけ。
その中からmaxを選ぶ。
-B. [AC] Great Ocean View
問題ページ【B - Great Ocean View】
私のコンテスト中の解法(解説も同じような解法)
# -*- coding: utf-8 -*- N = int(input()) lst = list(map(int,input().split())) max = 0 sum = 0 for i in range(N): if max <= lst[i]: sum = sum + 1 max = lst[i] print(sum)
実行時間:17 ms メモリ:2940 KB
山の数分forループをまわして、西から山を調べていく。
高い山をmaxに代入して、maxを更新していくたびにsumを増やす。
-C. [AC] Coloring Colorfully
問題ページ【C - Coloring Colorfully】
私のコンテスト中の解法
# -*- coding: utf-8 -*- *s, = map(int,input()) if len(s)%2 == 0: s0 = [0,1] *(len(s)//2) s1 = [1,0] *(len(s)//2) else: s0 = [0,1] *(len(s)//2) s0.append(0) s1 = [1,0] *(len(s)//2) s1.append(1) sum0 = 0 sum1 = 0 for i in range(len(s)): if s0[i] != s[i]: sum0 = sum0 +1 if s1[i] != s[i]: sum1 = sum1 +1 ans = min(sum0, sum1) print(ans)
実行時間:69 ms メモリ:5620 KB
Sと同じ長さのタイル列を、白黒白・・・と黒白黒・・・の2つ生成する。
Sとその2つのタイル列を比較して、違うとことがあったらsumを足していく。
最後に違いが少なかった方を出力。
解説の解法
# -*- coding: utf-8 -*- *s, = map(int,input()) # 初期値は大きい値 ans = len(s) # iは最初に使う色(0 or 1) for i in range(2): # 変える必要があるタイル数 cnt = 0 for j in range(len(s)): if (j % 2 == 0) ^ (s[j] == i): cnt = cnt +1 ans = min(ans, cnt) print(ans)
実行時間:82 ms メモリ:3956 KB
はじめが0だったら偶数番目は0になり、
はじめが1だったら偶数番目は1になる。
^(XOR)を使っている。
偶数番目が i のとき、あっているのでcntはそのまま。
偶数番目が i ではないとき、変える必要があるのでcntは増える。←
奇数番目が i のとき、変える必要があるのでcntは増える。←
奇数番目が i ではないとき、あっているのでcntはそのまま。
j % 2 == 0 | s[j] == i | 0 or 1 =============================== True | True | 0 True | False | 1 ← False | True | 1 ← False | False | 0