結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 6 ms
7,428 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 72 ms
44,344 KB
testcase_06 AC 7 ms
7,624 KB
testcase_07 AC 4 ms
4,532 KB
testcase_08 AC 27 ms
17,884 KB
testcase_09 AC 405 ms
81,672 KB
testcase_10 AC 1,044 ms
81,668 KB
testcase_11 AC 253 ms
81,656 KB
testcase_12 AC 59 ms
38,200 KB
testcase_13 AC 5 ms
5,072 KB
testcase_14 AC 879 ms
81,528 KB
testcase_15 AC 2,407 ms
81,664 KB
testcase_16 AC 1,341 ms
81,416 KB
testcase_17 AC 2,115 ms
81,424 KB
testcase_18 AC 4 ms
4,500 KB
testcase_19 AC 2,756 ms
81,528 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 923 ms
81,468 KB
testcase_23 AC 2,779 ms
81,476 KB
testcase_24 AC 2,776 ms
81,504 KB
testcase_25 AC 2,395 ms
81,484 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 6 ms
5,528 KB
testcase_28 AC 1,257 ms
81,484 KB
testcase_29 AC 265 ms
81,412 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 169 ms
81,480 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 n) {
	int count = 0;
	while (n > 0) {
		count += n % 2;
		n /= 2;
	}
	return count;
}

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