def test_all(f):
for i, data in enumerate(eval(f'test_data{f.__name__[0]}')):
exp = data["out"]
ans = f(*data["in"])
result = "AC" if exp == ans else "WA"
print(f"{i+1} {result}: expected: {exp}, output: {ans}")
from math import log10, ceil
def B_ng(X, K):
keta = ceil(log10(X))
X /= 10 ** keta
for i in range(K):
X = round(X, keta - 1 - i)
return int(X * 10 ** keta)
from decimal import Decimal, ROUND_HALF_UP
for i in range(10):
x = 0.05 + i / 10
dec = Decimal(f'{x:.2f}')
print(dec, dec.quantize(Decimal(".0"), rounding=ROUND_HALF_UP))
decimalを使った提出
from decimal import Decimal, ROUND_HALF_UP
def B(X, K):
X /= 10 ** (K + 1)
X = Decimal(str(X))
for i in range(K):
X = X.quantize(Decimal(10) ** -(K - i), rounding=ROUND_HALF_UP)
return int(X * 10 ** (K + 1))test_all(B)
from io import StringIO
def C(N, A):
out = StringIO()
A2 = {a:i for i, a in enumerate(sorted(set(A)), 1)}
K = {i: 0 for i in range(N)}
for a in A:
K[len(A2) - A2[a]] += 1
for v in K.values():
print(v, file=out)
return out.getvalue()test_all(C)
from io import StringIO
def C(N, A):
out = StringIO()
A3 = {}
for a in A:
if a in A3: A3[a] += 1
else: A3[a] = 1
for a in sorted(A3)[::-1]:
print(A3[a], file=out)
print("0\n" * (N - len(A3)), end="", file=out)
return out.getvalue()test_all(C)
def D(H, W, rs, cs, N, rc, Q, dl):
out = StringIO()
def check(r, c):
return 1 <= r <= H and 1 <= c <= W and [r, c] not in rc
now = rs, cs
for d, l in dl:
for _ in range(int(l)):
r, c = now
if d == "L":
nxt = [r, c - 1]
elif d == "R":
nxt = [r, c + 1]
elif d == "U":
nxt = [r - 1, c]
elif d == "D":
nxt = [r + 1, c]
if check(*nxt):
now = nxt
continue
break
print(*now, file=out)
return out.getvalue()test_all(D)
from operator import add, sub
def D(H, W, rs, cs, N, rc, Q, dl):
out = StringIO()
grid = [[[] for _ in range(W)] for _ in range(H)]
for r, c in rc:
grid[r-1][c-1] = None
def preproc(grid, rev=False):
r = len(grid)
c = len(grid[0])
range_c = range(c - 1, -1, -1) if rev else range(c)
op = sub if rev else add
for i in range(r):
ava = 0 if not rev else c - 1
for j in range_c:
if grid[i][j] is None:
ava = op(j, 1)
else:
grid[i][j].append(ava)
preproc(grid)
preproc(grid, rev=True)
*grid, = zip(*grid)
preproc(grid)
preproc(grid, rev=True)
*grid, = zip(*grid)
# print(*grid, sep="\n")
nr, nc = (rs - 1, cs - 1)
for d, l in dl:
l = int(l)
if d == "L":
if nc - grid[nr][nc][0] <= l:
nc = grid[nr][nc][0]
else:
nc -= l
elif d == "R":
if grid[nr][nc][1] - nc <= l:
nc = grid[nr][nc][1]
else:
nc += l
elif d == "U":
if nr - grid[nr][nc][2] <= l:
nr = grid[nr][nc][2]
else:
nr -= l
elif d == "D":
if grid[nr][nc][3] - nr <= l:
nr = grid[nr][nc][3]
else:
nr += l
print(nr + 1, nc + 1, file=out)
return out.getvalue()test_all(D)
from bisect import bisect
def D(H, W, rs, cs, N, rc, Q, dl):
out = StringIO()
def search(a, x, rev=False):
idx = bisect(a, x)
return a[idx] - 1 if not rev else a[idx - 1] + 1
vertical = {}
horizontal = {}
for r, c in rc:
if c in vertical:
vertical[c].append(r)
else:
vertical[c] = [r]
if r in horizontal:
horizontal[r].append(c)
else:
horizontal[r] = [c]
vertical = {k: sorted([0] + v + [H + 1]) for k, v in vertical.items()}
horizontal = {k: sorted([0] + h + [W + 1]) for k, h in horizontal.items()}
rn, cn = rs, cs
for d, l in dl:
l = int(l)
if d == "L":
if rn not in horizontal:
c = 1
else:
c = search(horizontal[rn], cn, rev=True)
dist = abs(cn - c)
cn = c if dist <= l else cn - l
elif d == "R":
if rn not in horizontal:
c = W
else:
c = search(horizontal[rn], cn)
dist = abs(cn - c)
cn = c if dist <= l else cn + l
elif d == "U":
if cn not in vertical:
r = 1
else:
r = search(vertical[cn], rn, rev=True)
dist = abs(rn - r)
rn = r if dist <= l else rn - l
elif d == "D":
if cn not in vertical:
r = H
else:
r = search(vertical[cn], rn)
dist = abs(rn - r)
rn = r if dist <= l else rn + l
print(rn, cn, file=out)
# print(rn, cn)
return out.getvalue()test_all(D)