結果

問題 No.2716 Falcon Method
ユーザー dp_ijk
提出日時 2024-04-05 22:36:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 645 ms / 2,000 ms
コード長 824 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 82,344 KB
実行使用メモリ 114,516 KB
最終ジャッジ日時 2024-10-01 02:46:16
合計ジャッジ時間 10,235 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import accumulate
from bisect import bisect_left, bisect_right

N, Q = map(int, input().split())
INF = float("INF")

S = [*input()]
D = [int(ch == "D") for ch in S]
R = [int(ch == "R") for ch in S]

Dwa = [0] + [*accumulate(D)]
Rwa = [0] + [*accumulate(R)]

DELTA = sum(D)
RHO = sum(R)


def estimate_time(X, P, PI, wa):
   if PI == 0:
      return INF
   t = 0
   q, r = divmod(X, PI)
   t += q*N
   X = r

   tail = wa[N] - wa[P]
   if tail < X:
      X -= tail
      t += N-P
      i = 0
   else:
      i = P
   j = bisect_left(wa, wa[i] + X)
   t += j-i
   return t


def solve(H, W, P):
   t_D = estimate_time(H, P, DELTA, Dwa)
   t_R = estimate_time(W, P, RHO, Rwa)
   t = min(t_D, t_R)
   return (P+t)%N


for _ in range(Q):
   H, W, P = map(int, input().split())
   ans = solve(H, W, P)
   print(ans)
0