結果

問題 No.3091 The Little Match Boy
コンテスト
ユーザー ygussany
提出日時 2025-03-09 13:24:13
言語 C
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 906 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 180 ms
コンパイル使用メモリ 39,360 KB
最終ジャッジ日時 2026-02-22 13:06:33
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 62
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <stdio.h>

typedef struct HList {
	struct HList *next;
	int i, j;
} hlist;

#define HASH 5000011
const int H_Mod = HASH;

int hash_func(int i, int j)
{
	return (((long long)i << 30) + j) % H_Mod;
}

int main()
{
	int i, N, M, S[100001];
	scanf("%d %d", &N, &M);
	for (i = 1; i <= M; i++) scanf("%d", &(S[i]));
	
	int hn = 0, h, ii, jj, p[100001];
	hlist *hash[HASH] = {}, hd[100001], *hp;
	for (i = 1; i <= N; i++) p[i] = i;
	for (i = 1; i <= M; i++) {
		p[S[i]] ^= p[S[i]+1];
		p[S[i]+1] ^= p[S[i]];
		p[S[i]] ^= p[S[i]+1];

		ii = p[S[i]];
		jj = p[S[i]+1];
		if (ii > jj) {
			ii ^= jj;
			jj ^= ii;
			ii ^= jj;
		}
		h = hash_func(ii, jj);
		for (hp = hash[h]; hp != NULL; hp = hp->next) if (hp->i == ii && hp->j == jj) break;
		if (hp == NULL) {
			hd[hn].i = ii;
			hd[hn].j = jj;
			hd[hn].next = hash[h];
			hash[h] = &(hd[hn++]);
		}
	}
	printf("%d\n", hn);
	fflush(stdout);
	return 0;
}
0