結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-02-11 13:13:04 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 5,000 ms |
| コード長 | 952 bytes |
| コンパイル時間 | 1,739 ms |
| コンパイル使用メモリ | 176,616 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 09:38:53 |
| 合計ジャッジ時間 | 2,782 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
const int INF=1e9;
int main(){
int n;cin >> n;
vector<int> d(n+1,INF);
vector<vector<int>> g(n+1);
for(int i=1;i<=n;i++){
int cnt=0;
for(int j=0;j<32;j++){
if((i>>j)&1){
cnt++;
}
}
if(i-cnt>=1){
g[i].push_back(i-cnt);
}
if(i+cnt<=n){
g[i].push_back(i+cnt);
}
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
d[1]=1;
pq.push({0,1});
while(!pq.empty()){
int u=pq.top().first,v=pq.top().second;
pq.pop();
if(d[v]<u){
continue;
}
for(auto x:g[v]){
if(d[x]>d[v]+1){
d[x]=d[v]+1;
pq.push({d[x],x});
}
}
}
if(d[n]==INF){
cout << -1 << endl;
}
else{
cout << d[n] << endl;
}
}