結果

問題 No.3244 Range Multiple of 8 Query
ユーザー 👑 ygussany
提出日時 2025-08-16 13:25:00
言語 C
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,536 bytes
コンパイル時間 257 ms
コンパイル使用メモリ 28,244 KB
実行使用メモリ 16,000 KB
最終ジャッジ日時 2025-08-16 13:25:10
合計ジャッジ時間 9,856 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 19 WA * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
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]));
      |                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#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++) {
		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;
}
0