結果

問題 No.3 ビットすごろく
ユーザー assy1028assy1028
提出日時 2015-03-20 18:48:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,390 bytes
コンパイル時間 1,593 ms
コンパイル使用メモリ 82,840 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 23:05:24
合計ジャッジ時間 2,165 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <complex>
#include <string.h>
using namespace std;

#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define uniq(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef pair<int, int> P;
typedef unsigned int uint;

const int inf = 1000000009;

int bitcount(int x) {
    int ret = 0;
    while (x) {
	x &= x - 1;
	ret++;
    }
    return ret;
}

const int MAX = 10010;
struct edge {
    int to, cost;
};

int V;
vector<edge> G[MAX];
int d[MAX];

void dijkstra(int s) {
    priority_queue<P, vector<P>, greater<P> > que;
    fill(d, d+V, inf);
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
	P p = que.top(); que.pop();
	int v = p.second;
	if (d[v] < p.first) continue;
	for (int i = 0; i < G[v].size(); i++) {
	    edge e = G[v][i];
	    if (d[e.to] > d[v] + e.cost) {
		d[e.to] = d[v] + e.cost;
		que.push(P(d[e.to], e.to));
	    }
	}
    }
}


int main() {
    int n;
    scanf("%d", &n);
    V = n + 1;
    for (int i = 1; i <= n; i++) {
	int c = bitcount(i);
	if (i - c >= 1) G[i].push_back(edge{i-c, 1});
	if (i + c <= n) G[i].push_back(edge{i+c, 1});
    }
    dijkstra(1);

    printf("%d\n", d[n] < inf ? d[n]+1 : -1);
}
0