結果
| 問題 |
No.3244 Range Multiple of 8 Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-08-22 23:27:54 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 899 bytes |
| コンパイル時間 | 378 ms |
| コンパイル使用メモリ | 82,244 KB |
| 実行使用メモリ | 96,392 KB |
| 最終ジャッジ日時 | 2025-08-22 23:28:30 |
| 合計ジャッジ時間 | 18,994 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 19 TLE * 1 -- * 20 |
ソースコード
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+=[*str(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
bs=[-1]*10
for i in range(10):
bs[i]=bisect(idx[str(i)],r-1)-1
for st in e:
if m!=len(st):
continue
p=defaultdict(int)
a=0
pos=r-1
mv=[]
for j in range(m):
ns=st[~j]
k=bs[int(ns)]#bisect(idx[ns],r-1)-1
if 0<=k-p[ns]<len(idx[ns])and l<=idx[ns][k-p[ns]]:
nxt=idx[ns][k-p[ns]]
for t in mv:
if t<idx[ns][k-p[ns]]:
nxt-=1
a+=abs(pos-nxt)
mv+=idx[ns][k-p[ns]],
p[ns]+=1
else:
a=INF
break
pos-=1
ans=min(ans,a)
print([-1,ans][ans<INF])