結果
| 問題 |
No.3244 Range Multiple of 8 Query
|
| コンテスト | |
| ユーザー |
Kude
|
| 提出日時 | 2025-08-22 22:14:41 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,213 ms / 5,000 ms |
| コード長 | 2,246 bytes |
| コンパイル時間 | 3,601 ms |
| コンパイル使用メモリ | 310,128 KB |
| 実行使用メモリ | 7,716 KB |
| 最終ジャッジ日時 | 2025-08-22 22:15:15 |
| 合計ジャッジ時間 | 23,178 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 40 |
ソースコード
#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
} int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
string s;
cin >> s;
reverse(all(s));
VI pos[10];
rep(i, n) pos[s[i] - '0'].emplace_back(i);
vector<array<int, 3>> cand;
for (int x = 0; x < 1000; x += 8) {
string s = to_string(x);
if (s.size() != 3 || count(all(s), '0')) continue;
reverse(all(s));
cand.push_back({s[0] - '0', s[1] - '0', s[2] - '0'});
}
constexpr int INF = 1001001001;
rep(_, q) {
int l, r;
cin >> l >> r;
l--;
tie(l, r) = pair(n - r, n - l);
if (r - l < 3) {
if (r - l == 1) {
cout << (s[l] == '8' ? 0 : -1) << '\n';
} else {
int x = s[l] - '0', y = s[l+1] - '0';
int ans = (10 * y + x) % 8 == 0 ? 0 : (10 * x + y) % 8 == 0 ? 1 : -1;
cout << ans << '\n';
}
continue;
}
span<int> d[10];
rep(x, 10) d[x] = span(ranges::lower_bound(pos[x], l), ranges::lower_bound(pos[x], r));
int ans = INF;
int nxt[10]{};
for (auto a : cand) {
rep(i, 3) nxt[a[i]] = 0;
int p[3]{};
bool ok = true;
rep(i, 3) {
int x = a[i];
if (nxt[x] == ssize(d[x])) {
ok = false;
break;
}
p[i] = d[x][nxt[x]++] - l;
}
if (!ok) continue;
int cost = 0;
rep(i, 3) {
for (int j = i + 1; j < 3; j++) if (p[j] < p[i]) p[j]++;
cost += p[i] - i;
}
chmin(ans, cost);
}
if (ans == INF) ans = -1;
cout << ans << '\n';
}
}
Kude