結果
| 問題 |
No.3244 Range Multiple of 8 Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-08-22 22:20:56 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 758 ms / 5,000 ms |
| コード長 | 2,838 bytes |
| コンパイル時間 | 833 ms |
| コンパイル使用メモリ | 105,780 KB |
| 実行使用メモリ | 39,892 KB |
| 最終ジャッジ日時 | 2025-08-22 22:21:26 |
| 合計ジャッジ時間 | 17,763 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 40 |
ソースコード
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_reverse (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;
if (R - L == 1) {
const a0 = S[L] - '0';
if (a0 % 8 == 0) chmin(ans, 0);
} else if (R - L == 2) {
const a0 = S[L] - '0';
const a1 = S[L + 1] - '0';
if ((a0 * 10 + a1) % 8 == 0) chmin(ans, 0);
if ((a1 * 10 + a0) % 8 == 0) chmin(ans, 1);
} else {
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) {
}
}