結果
問題 | No.3 ビットすごろく |
ユーザー |
|
提出日時 | 2021-04-20 14:24:26 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 1,835 bytes |
コンパイル時間 | 1,620 ms |
コンパイル使用メモリ | 174,368 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-04 05:20:05 |
合計ジャッジ時間 | 2,477 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <bits/stdc++.h>// #include <atcoder/all>// #include "icld.cpp"using namespace std;using ll = long long int;using vi = vector<int>;using si = set<int>;using vll = vector<ll>;using vvi = vector<vector<int>>;using ss = string;using db = double;template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>;const int dx[4] = {1,0,-1,0};const int dy[4] = {0,1,0,-1};#define V vector#define P pair<int,int>#define PLL pair<ll,ll>#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)#define rev(i,s,n) for(int i=(s);i>=(int)(n);i--)#define reciv(v,n) vll (v)((n)); rep(i,0,(n))cin>>v[i]#define all(v) v.begin(),v.end()#define rall(v) v.rbegin(),v.rend()#define ci(x) cin >> x#define cii(x) ll x;cin >> x#define cci(x,y) ll x,y;cin >> x >> y#define co(x) cout << x << endl#define pb push_back#define eb emplace_back#define rz resize#define pu push#define sz(x) int(x.size())#define vij v[i][j]// ll p = 1e9+7;// ll p = 998244353;// n do -> n*pi/180#define yn cout<<"Yes"<<endl;else cout<<"No"<<endl#define YN cout<<"YES"<<endl;else cout<<"NO"<<endltemplate<class T>void chmax(T &x,T y){x=max(x,y);}template<class T>void chmin(T &x,T y){x=min(x,y);}int bt(int x){return __builtin_popcount(x);}int main(){cii(n);int inf=1001001001;int ans=-1;vi dp(n+1,inf);dp[1]=1;minpq<P>q;q.pu(P(1,1));while(sz(q)){int dist,x;tie(dist,x)=q.top();q.pop();if(dp[x]<dist)continue;if(x==n){ans=dp[x];break;}if(dp[x]==n){ans=-1;break;}int go=bt(x);if(1<=x-go){if(dp[x]+1<dp[x-go]){dp[x-go]=dp[x]+1;q.pu(P(dp[x-go],x-go));}}if(x+go<=n){if(dp[x]+1<dp[x+go]){dp[x+go]=dp[x]+1;q.pu(P(dp[x+go],x+go));}}}co(ans);}