結果

問題 No.3 ビットすごろく
ユーザー laftlaft
提出日時 2019-02-21 12:32:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,610 bytes
コンパイル時間 2,294 ms
コンパイル使用メモリ 209,208 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 01:12:37
合計ジャッジ時間 3,469 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
using ll = long long; using ull = unsigned long long;
using vb = vector<bool>;
using vi = vector<int>; using vvi = vector<vector<int>>;
using vl = vector<ll>; using vvl = vector<vector<ll>>;
using pii = pair<int,int>; using pll = pair<ll,ll>;
#define FOR(i,a,b) for(ll i = (a); i < (ll)(b); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define eb(val) emplace_back(val)
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1e9+7;
const ll INF = 1e10;
//#define int ll
void cioacc(){//accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
struct edge{
  int to,cost;
};
bool operator>(edge left,edge right){
  return left.cost > right.cost;
}
using vve = vector<vector<edge>>;
vl dijkstra(vve &gr,int st = 0){
  int n = gr.size();
  vl dist(n,INF);
  dist[st] = 0;
  priority_queue<pii,vector<pii>,greater<pii>> q;
  q.push(make_pair(dist[st]=0,st));
  while(!q.empty()){
    int now = q.top().second;
    q.pop();
    REP(i,gr[now].size()){
      edge &e = gr[now][i];
      if(dist[now]+e.cost<dist[e.to]){
        dist[e.to] = dist[now]+e.cost;
        q.push({dist[e.to],e.to});
      }
    }
  }
  return dist;
}
signed main(){
  int n;
  cin >> n;
  vve gr(n);
  REP(i,n){
    int now = i+1;
    bitset<32> bits(i+1);
    int dis = (int)bits.count();
    if(now-dis>=1) gr[i].emplace_back(edge{now-1-dis,1});
    if(now+dis<=n) gr[i].emplace_back(edge{now-1+dis,1});
  }
  auto dist = dijkstra(gr);
  cout << (dist[n-1]==INF?-1:dist[n-1]+1) << endl;
}
0