結果
問題 | No.3 ビットすごろく |
ユーザー | shiratama |
提出日時 | 2017-05-06 16:23:13 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 471 ms / 5,000 ms |
コード長 | 979 bytes |
コンパイル時間 | 732 ms |
コンパイル使用メモリ | 66,840 KB |
実行使用メモリ | 8,320 KB |
最終ジャッジ日時 | 2024-07-01 08:42:39 |
合計ジャッジ時間 | 5,534 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <iostream> #include <limits> #include <queue> #include <vector> struct Step { int curr_n; int cost; }; auto count_one(int n) -> int { int count = 0; for (int b = 1; b <= 10000; b <<= 1) { if ((n & b) != 0) { count++; } } return count; } auto main() -> int { int last_n; std::cin >> last_n; std::queue<Step> steps; std::vector<int> costs(last_n, std::numeric_limits<int>::max()); steps.push({1, 1}); while (!steps.empty()) { Step s = steps.front(); steps.pop(); if (s.cost <= costs[s.curr_n - 1]) { costs[s.curr_n - 1] = s.cost; int n_one = count_one(s.curr_n); if ((s.curr_n + n_one) <= last_n) { steps.push({s.curr_n + n_one, s.cost + 1}); } if ((s.curr_n - n_one) >= 1) { steps.push({s.curr_n - n_one, s.cost + 1}); } } } int smallestCost = costs[last_n - 1]; if (smallestCost == std::numeric_limits<int>::max()) { std::cout << -1 << std::endl; } else { std::cout << smallestCost << std::endl; } }