結果
| 問題 |
No.3244 Range Multiple of 8 Query
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2025-08-16 13:28:28 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 331 ms / 5,000 ms |
| コード長 | 1,901 bytes |
| コンパイル時間 | 405 ms |
| コンパイル使用メモリ | 28,348 KB |
| 実行使用メモリ | 15,872 KB |
| 最終ジャッジ日時 | 2025-08-16 13:28:37 |
| 合計ジャッジ時間 | 8,611 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 40 |
コンパイルメッセージ
main.c: In function ‘main’:
main.c:12:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
12 | scanf("%d %d", &N, &Q);
| ^~~~~~~~~~~~~~~~~~~~~~
main.c:13:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
13 | scanf("%s", &(S[1]));
| ^~~~~~~~~~~~~~~~~~~~
main.c:14:34: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
14 | for (i = 1; i <= Q; i++) scanf("%d %d", &(l[i]), &(r[i]));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <stdio.h>
void chmin(int *a, int b)
{
if (*a > b) *a = b;
}
int main()
{
int i, N, Q, l[300000], r[300000];
char S[300000];
scanf("%d %d", &N, &Q);
scanf("%s", &(S[1]));
for (i = 1; i <= Q; i++) scanf("%d %d", &(l[i]), &(r[i]));
int j, k, cand[100][3];
for (j = 104, k = 0; j < 1000; j += 8) {
if (j % 10 == 0 || j / 10 % 10 == 0) continue;
cand[++k][0] = j / 100;
cand[k][1] = j / 10 % 10;
cand[k][2] = j % 10;
}
int last[300000][10], x[3], ans, tmp;
for (j = 1; j <= 9; j++) last[0][j] = 0;
for (i = 1; i <= N; i++) {
for (j = 1; j <= 9; j++) last[i][j] = last[i-1][j];
last[i][S[i] - '0'] = i;
}
for (i = 1; i <= Q; i++) {
if (l[i] == r[i]) {
if (S[l[i]] == '8') printf("0\n");
else printf("-1\n");
} else if (l[i] == r[i] - 1) {
tmp = (S[l[i]] - '0') * 10 + S[r[i]] - '0';
if (tmp % 8 == 0) printf("0\n");
else {
tmp = (S[r[i]] - '0') * 10 + S[l[i]] - '0';
if (tmp % 8 == 0) printf("1\n");
else printf("-1\n");
}
} else {
for (j = 1, ans = N * 3; j <= k; j++) {
x[2] = last[r[i]][cand[j][2]];
if (x[2] < l[i]) continue;
if (cand[j][1] == cand[j][2]) x[1] = last[x[2]-1][cand[j][1]];
else x[1] = last[r[i]][cand[j][1]];
if (x[1] < l[i]) continue;
if (cand[j][0] == cand[j][1]) x[0] = last[x[1]-1][cand[j][0]];
else if (cand[j][0] == cand[j][2]) x[0] = last[x[2]-1][cand[j][0]];
else x[0] = last[r[i]][cand[j][0]];
if (x[0] < l[i]) continue;
x[0] = r[i] - x[0];
x[1] = r[i] - x[1];
x[2] = r[i] - x[2];
tmp = x[2] + x[1] - 1 + x[0] - 2;
if (x[1] < x[2]) tmp++;
if (x[0] < x[1]) tmp++;
if (x[0] < x[2]) tmp++;
chmin(&ans, tmp);
// printf("%d %d %d: %d %d %d: %d\n", cand[j][0], cand[j][1], cand[j][2], x[0], x[1], x[2], tmp);
}
if (ans == N * 3) printf("-1\n");
else printf("%d\n", ans);
}
}
fflush(stdout);
return 0;
}