結果

問題 No.3244 Range Multiple of 8 Query
ユーザー 👑 hos.lyric
提出日時 2025-08-22 22:11:20
言語 D
(dmd 2.109.1)
結果
WA  
実行時間 -
コード長 2,465 bytes
コンパイル時間 2,000 ms
コンパイル使用メモリ 107,640 KB
実行使用メモリ 39,896 KB
最終ジャッジ日時 2025-08-22 22:12:55
合計ジャッジ時間 17,183 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 4 WA * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }

string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum INF = 1001001001;

void main() {
  int[3][] A;
  for (int x = 0; x < 1000; x += 8) A ~= [x % 10, x / 10 % 10, x / 100];
  
  try {
    for (; ; ) {
      const N = readInt;
      const Q = readInt;
      const S = readToken;
      
      auto prv = new int[3][10][N + 1];
      foreach (a; 0 .. 10) foreach (k; 0 .. 3) prv[0][a][k] = -1;
      foreach (i; 0 .. N) {
        prv[i + 1] = prv[i];
        const a = S[i] - '0';
        foreach (k; 1 .. 3) prv[i + 1][a][k] = prv[i + 1][a][k - 1];
        prv[i + 1][a][0] = i;
      }
      
      foreach (q; 0 .. Q) {
        const L = readInt - 1;
        const R = readInt;
        debug writeln(prv[R]);
        
        int ans = INF;
        foreach (a; A) {
          const i0 = prv[R][a[0]][0];
          const i1 = prv[R][a[1]][(a[0] == a[1])];
          const i2 = prv[R][a[2]][(a[0] == a[2]) + (a[1] == a[2])];
          if (L <= i0 && L <= i1 && L <= i2) {
            int cost = 0;
            cost += ((R - 1) - i0);
            cost += ((R - 2) - (i1 - (i0 < i1)));
            cost += ((R - 3) - (i2 - (i0 < i2) - (i1 < i2)));
            debug writeln(a, ": ", cost);
            chmin(ans, cost);
          }
        }
        writeln((ans < INF) ? ans : -1);
      }
    }
  } catch (EOFException e) {
  }
}
0