3で割ればいい。(余り切り捨て)
a = int(input()) print(a // 3)
Pythonなら、set()
で配列を重複なしの集合に変換できる。この重複なしの集合の長さが3か4か見ればよい。
n = input() s = set(input().split()) print('Three' if len(s) == 3 else 'Four')
もしくは、色が決まっているので、'Y'が入っているかどうかで見分けられる。
n = input() s = input().split() print('Four' if 'Y' in s else 'Three')
選ぶ3人の頭文字がどうなっているかというと、「'M','A','R'」や「'A','C','H'」のように'MARCH'の中から選べる異なる3文字となっている。
たとえば頭文字「'M','A','R'」に絞って考えると、それぞれの文字で始まる人が「5人、9人、4人」だった場合、組み合わせの数は $5 \times 9 \times 4=180$ となる。これを全ての異なる3文字の組み合わせで調べれて足しあわせればよい。ただし各頭文字で始まる人がいなかった場合にKeyErrorに注意。
Pythonでは、ある頭文字で始まる人が何人いるかは、collections.Counterを使って数え上げられる。 また、5個の中から異なる3個を取る組み合わせの列挙は、itertools.combinationsを使える。
from collections import Counter from itertools import combinations n = int(input()) l = [input()[0] for _ in range(n)] lc = Counter(l) con = [] for c in 'MARCH': if c in lc: con.append(lc[c]) ans = 0 for a, b, c in combinations(con, 3): ans += a * b * c print(ans)
理解にちょっと手間取る。
$Q$ の制約が $10^5$ と割と大きいので、1個ずつのクエリを高速に求められるように事前計算する、というのがこの問題の肝。
こういうのは累積和の差分で計算させるとよい。
HxW=5x5 D=4, L=2, R=14 2 6 10 14 18 22 dist[L] ○→→○→→○→→○→→○→→○ dist[R] ○→→○→→○ dist[L]-dist[R] ○→→○→→○→→○
※AtCoderの解説pdfや解説資料では、逆に始点からの累積距離を事前計算していたので、混同に注意。
import sys h, w, d = map(int, input().split()) f = [list(map(int, input().split())) for _ in range(h)] c_map = [0] * (h * w) for i in range(h): for j in range(w): c_map[f[i][j] - 1] = (i, j) d_map = [0] * (h * w) for l in range(h * w - d - 1, -1, -1): i, j = c_map[l] i2, j2 = c_map[l + d] d_map[l] = d_map[l + d] + abs(i - i2) + abs(j - j2) buf = [] q = int(input()) for line in sys.stdin.readlines(): l, r = map(int, line.split()) buf.append(d_map[l - 1] - d_map[r - 1]) print('\n'.join(map(str, buf)))