C. Amr and Chemistry(bfs)
作者:互联网
https://codeforces.com/problemset/problem/558/C
思路:直接暴力把每个点能到值都扫出来,然后遍历看哪个值==n取最小就好。
https://www.luogu.com.cn/problem/solution/CF558C看了一下对于*2和/2的可以直接建树做跑左右儿子做+换根dp O(n)
但是应该比较针对*2和/2把
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef int LL;
typedef pair<LL,LL>P;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL sum[maxn];
LL cnt[maxn];
LL vis[maxn];
int main(void){
cin.tie(0);std::ios::sync_with_stdio(false);
LL n;cin>>n;
for(LL i=1;i<=n;i++){
LL x;cin>>x;
queue<P>que;
que.push({x,0});
while(!que.empty()){
P now=que.front();
LL x=now.first;LL step=now.second;que.pop();
if(x>1e5+100) continue;
if(vis[x]==i) continue;
vis[x]=i;
sum[x]+=step;
cnt[x]++;
if(x) que.push({x*2,step+1});
if(x) que.push({x/2,step+1});
}
}
LL ans=0x3f3f3f3f;
for(LL i=0;i<maxn;i++){
if(cnt[i]==n){
ans=min(ans,sum[i]);
}
}
cout<<ans<<"\n";
return 0;
}
标签:Amr,Chemistry,LL,bfs,vis,step,ch,que,include 来源: https://blog.csdn.net/zstuyyyyccccbbbb/article/details/115618290