挺有意思的一个题

要想用时少就要走的快,走的快就要一步走的远。所以慢慢从大到小枚举怎么走,因为不能回头所以说要正好

这里要么直接直接走要么走传送门且走一个传送门最好

每次走的时候都需要编写一个函数进行时间的统计,并且传送门需要min

代码

#include <bits/stdc++.h>
using namespace std;
double sum[66];
long long cost(long long st,long long ed)
{
  long long ans=0;
  while(st<ed)
  {
    for(int i=30;i>=0;i--)
    if(st+sum[i]<=ed)
    {
      st+=sum[i];
      ans++;
      break;
    }
  }
  return ans;
}
int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n,k;
  cin>>n>>k;
  for(int i=0;i<=30;i++)
  sum[i]=pow(2,i);
  long long ans=cost(1,n);
  while(k--)
  {
    long long st,ed;
    cin>>st>>ed;
    if(st>ed)
    swap(st,ed);
    if(st==ed)
    continue;
    ans=min(ans,cost(1,st)+1+cost(ed,n));
  }
  cout<<ans;
}

相关文章:

  • 2021-12-07
  • 2021-06-06
  • 2021-12-20
  • 2021-05-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-15
  • 2022-12-23
  • 2021-04-03
  • 2021-09-23
  • 2021-12-03
  • 2021-11-18
相关资源
相似解决方案