結果

問題 No.3244 Range Multiple of 8 Query
ユーザー moon17
提出日時 2025-08-22 23:09:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 683 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 82,616 KB
実行使用メモリ 77,720 KB
最終ジャッジ日時 2025-08-22 23:10:56
合計ジャッジ時間 10,009 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 1 WA * 16 TLE * 1 -- * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import*
from collections import*
n,q=map(int,input().split())
s=input()
e=[]
for i in range(1000):
  if i%8==0 and'0'not in str(i):
    e+=i,
idx=defaultdict(list)
for i in range(n):
  idx[s[i]]+=i,
INF=1<<60
for _ in range(q):
  l,r=map(int,input().split())
  l-=1
  d=r-l
  m=min(d,3)
  ans=INF
  for i in e:
    st=[*str(i)]
    if m!=len(st):
      continue
    p=defaultdict(int)
    a=0
    pos=r-1
    while st:
      ns=st.pop()
      k=bisect(idx[ns],pos)-1
      if 0<=k-p[ns]<len(idx[ns])and l<=idx[ns][k-p[ns]]:
        a+=pos-idx[ns][k-p[ns]]
        p[ns]+=1
      else:
        a=INF
        break
      pos-=1
    ans=min(ans,a)
  print([-1,ans][ans<INF])
0