結果

問題 No.3 ビットすごろく
ユーザー nemunemunemunemu
提出日時 2024-01-13 15:51:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,010 bytes
コンパイル時間 861 ms
コンパイル使用メモリ 77,948 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-01-13 15:51:39
合計ジャッジ時間 2,411 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 1 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:19:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   19 |         auto [d, x] = que.top();
      |              ^

ソースコード

diff #

// No.3 ビットすごろく
#include <iostream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
const int INF = 1 << 30;

int main() {
    int N;
    cin >> N;
    vector<int> dp(N, INF);
    dp[0] = 1;
    priority_queue<pair<int, int>,
                    vector<pair<int, int>>,
                    greater<pair<int, int>>> que;
    que.push({1, 0});
    while (!que.empty()) {
        auto [d, x] = que.top();
        que.pop();
        if (d > dp[x]) continue;
        int move = bitset<32>(x + 1).count();
        if (x + move < N) {
            if (dp[x + move] > dp[x] + 1) {
                dp[x + move] = dp[x] + 1;
                que.push({dp[x + move], x + move});
            }
        }
        if (x - move >= 0) {
            if (dp[x - move] > dp[x] + 1) {
                dp[x - move] = dp[x] + 1;
                que.push({dp[x - move], x - move});
            }
        }
    }
    if (dp[N - 1] == INF) cout << -1 << endl;
    else cout << dp[N - 1] << endl;
}
0