結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2016-09-14 02:28:41 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2,373 ms / 5,000 ms |
コード長 | 1,109 bytes |
コンパイル時間 | 427 ms |
コンパイル使用メモリ | 54,844 KB |
実行使用メモリ | 81,632 KB |
最終ジャッジ日時 | 2024-07-01 08:05:06 |
合計ジャッジ時間 | 20,583 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#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; }