結果

問題 No.3 ビットすごろく
ユーザー zenito9970zenito9970
提出日時 2017-03-26 19:29:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,052 bytes
コンパイル時間 1,774 ms
コンパイル使用メモリ 177,196 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 00:37:27
合計ジャッジ時間 3,107 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 3 ms
4,384 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define DUMP(x) cerr<<#x<<": "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<": "<<(x)<<" (L"<<__LINE__<<")"<<endl
using namespace std;
using vint = vector<int>;
using vdouble = vector<double>;
using vstring = vector<string>;
using ll = long long;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

constexpr int popcnt(int a) { return __builtin_popcount(a); }

template <class E>
using Graph = vector<vector<E>>;

struct Edge {
    int to, cost;
    Edge(int to, int cost): to(to), cost(cost) {}
};

template <class E>
void addEdge(Graph<E>& graph, int from, int to, int cost) {
    graph[from].push_back(E(to, cost));
}

template <class T, T INF>
struct Dijkstra {
    vector<T> res;

    template <class E>
    Dijkstra(const Graph<E>& graph, int s) {
        res = vector<T>(graph.size(), INF);
        using P = pair<T, int>; // cost, to
        priority_queue<P, vector<P>, greater<P>> Q;
        Q.push(P(0, s));
        res[s] = 0;
        while(!Q.empty()) {
            const T cost = Q.top().first;
            const int to = Q.top().second;
            Q.pop();
            if(res[to] < cost) continue;
            for(E e: graph[to]) {
                if(cost + e.cost < res[e.to]) {
                    res[e.to] = cost + e.cost;
                    Q.push(P(res[e.to], e.to));
                }
            }
        }
    }
};

int main() {
    int N; cin >> N;
    vint to(N);
    rep(i, N) to[i] = popcnt(i+1);
    Graph<Edge> graph(N);
    rep(i, N) {
        if(i + to[i] < N) addEdge(graph, i, i + to[i], 1);
        if(0 < i - to[i]) addEdge(graph, i, i - to[i], 1);
    }
    const int inf = 1e9;
    Dijkstra<int, inf> dijk(graph, 0);
    if(dijk.res[N-1] == inf) cout << -1 << endl;
    else cout << (dijk.res[N-1]+1) << endl;
    return 0;
}
0