結果

問題 No.642 Two Operations No.1
ユーザー beetbeet
提出日時 2018-07-26 13:54:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 890 bytes
コンパイル時間 3,396 ms
コンパイル使用メモリ 181,156 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 18:39:35
合計ジャッジ時間 3,132 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  using P = pair<Int, Int>;
  priority_queue<P, vector<P>, greater<P> > q;
  auto dist=
    [&](Int x){
      Int res=n+(1<<15);
      for(Int i=0;i<20;i++)
	if((x<<i)>=n) chmin(res,(x<<i)-n);
      return res;
    };
  
  map<Int, Int> dp;
  dp[1]=0;
  q.emplace(dist(1),1);
  while(!q.empty()){
    Int x=q.top().second;q.pop();
    if(x==n) break;
    if(!dp.count(x*2)||dp[x*2]>dp[x]+1){
      dp[x*2]=dp[x]+1;
      q.emplace(dp[x*2]+dist(x*2),x*2);
    }
    if(!dp.count(x-1)||dp[x-1]>dp[x]+1){
      dp[x-1]=dp[x]+1;
      q.emplace(dp[x-1]+dist(x-1),x-1);
    }
  }
  cout<<dp[n]<<endl;
  return 0;
}
0