結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-02-20 23:46:47 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 898 ms / 5,000 ms |
| コード長 | 671 bytes |
| コンパイル時間 | 517 ms |
| コンパイル使用メモリ | 58,344 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 07:17:23 |
| 合計ジャッジ時間 | 14,969 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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::cerr << "Hello" << std::endl;
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;
}