炫酷路径(牛客)

挺有意思的一个题

解法

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

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

每次走的时候都需要编写一个函数进行时间的统计,并且传送门需要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;
}

猜你喜欢

转载自www.cnblogs.com/baccano-acmer/p/10353804.html