結果

問題 No.3 ビットすごろく
ユーザー airuaiairuai
提出日時 2016-09-14 02:28:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,627 ms / 5,000 ms
コード長 1,109 bytes
コンパイル時間 422 ms
コンパイル使用メモリ 52,720 KB
実行使用メモリ 81,660 KB
最終ジャッジ日時 2023-09-14 00:07:30
合計ジャッジ時間 14,954 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 6 ms
7,428 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 49 ms
44,316 KB
testcase_06 AC 6 ms
7,432 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 18 ms
17,676 KB
testcase_09 AC 254 ms
81,468 KB
testcase_10 AC 671 ms
81,540 KB
testcase_11 AC 173 ms
81,524 KB
testcase_12 AC 39 ms
38,284 KB
testcase_13 AC 5 ms
5,024 KB
testcase_14 AC 562 ms
81,440 KB
testcase_15 AC 1,390 ms
81,424 KB
testcase_16 AC 825 ms
81,660 KB
testcase_17 AC 1,258 ms
81,504 KB
testcase_18 AC 4 ms
4,492 KB
testcase_19 AC 1,610 ms
81,480 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 589 ms
81,500 KB
testcase_23 AC 1,626 ms
81,484 KB
testcase_24 AC 1,627 ms
81,600 KB
testcase_25 AC 1,398 ms
81,600 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 5 ms
5,396 KB
testcase_28 AC 784 ms
81,576 KB
testcase_29 AC 170 ms
81,440 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 110 ms
81,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

#define MAX 10000000

int q[MAX][2];
int head = 0, tail = 0;

void enq(int p, int kind) {
	q[tail][0] = p;
	q[tail][1] = kind;
	++tail;
	if (tail >= MAX) {
		tail = 0;
	}
}

bool deq(int* p, int* kind) {
	if (head == tail) {
		return false;
	}
	else {
		*p = q[head][0];
		*kind = q[head][1];
		++head;
		if (head >= MAX) {
			head = 0;
		}
		return true;
	}
}

int BinaryCount(int a) {
	/*int count = 0;
	while (n > 0) {
		count += n % 2;
		n /= 2;
	}
	return count;*/

	int cnt = 0;
	while (a>0) {
		cnt++;
		a &= a - 1;
	}
	return cnt;

}

int main(void) {
	int n;
	cin >> n;
	
	bool* flag = new bool[n];
	for (int i = 0; i < n; ++i) {
		flag[i] = false;
	}

	int posi = 1, hosuu;
	int kind = 1;
	do {
		if (posi == n) {
			cout << kind << endl;
			return 0;
		}

		flag[posi - 1] = true;
		hosuu = BinaryCount(posi);
		if (posi - hosuu > 0 && !flag[posi - 1 - hosuu]) {
			enq(posi - hosuu, kind + 1);
		}
		if (posi + hosuu <= n && !flag[posi - 1 + hosuu]) {
			enq(posi + hosuu, kind + 1);
		}
	} while (deq(&posi, &kind));

	cout << "-1" << endl;

	return 0;
}
0