結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
cocomoff
|
| 提出日時 | 2019-08-07 20:14:57 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 806 bytes |
| コンパイル時間 | 1,369 ms |
| コンパイル使用メモリ | 163,268 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 09:27:11 |
| 合計ジャッジ時間 | 2,410 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e8;
int bitCount(int bits) {
int cnt = 0;
for(int mask = 1; mask != 0; mask <<= 1) {
if( (bits & mask) != 0 )
++cnt;
}
return cnt;
}
int main() {
int N;
cin >> N;
queue<int> q;
q.push(1);
vector<int> dp(N + 1, INF);
dp[1] = 1;
while (!q.empty()) {
int now = q.front(); q.pop();
int d = bitCount(now);
int l = now - d;
if (1 <= l && l <= N && dp[l] == INF) {
dp[l] = dp[now] + 1;
q.push(l);
}
int r = now + d;
if (1 <= r && r <= N && dp[r] == INF) {
dp[r] = dp[now] + 1;
q.push(r);
}
}
cout << (dp[N] == INF ? -1 : dp[N]) << endl;
}
cocomoff