結果
問題 | No.3 ビットすごろく |
ユーザー | tai96 |
提出日時 | 2015-02-20 23:33:19 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 950 ms / 5,000 ms |
コード長 | 635 bytes |
コンパイル時間 | 430 ms |
コンパイル使用メモリ | 57,132 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 07:15:13 |
合計ジャッジ時間 | 15,671 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <iostream> #include <cstring> #include <algorithm> int n; int dp[10001]; const int inf = 100000000; int countBit(int x){ int ret = 0; while (x > 0){ if (x & 1)ret++; x >>= 1; } return ret; } int main(){ std::cin >> n; for (int i = 0; i < 10001; i++)dp[i] = inf; dp[1] = 1; for (int i = 0; i < 10001; i++){ for (int j = 1; j <= n; j++){ if (dp[j] == inf)continue; int cnt = countBit(j); if (j + cnt <= n)dp[j + cnt] = std::min(dp[j + cnt], dp[j] + 1); if (j - cnt >= 1)dp[j - cnt] = std::min(dp[j - cnt], dp[j] + 1); } } std::cout << (dp[n] == inf ? -1 : dp[n]) << std::endl; return 0; }