关于空间复杂度求助
查看原帖
关于空间复杂度求助
456287
wuxingyuan楼主2023/4/4 15:40

由于根号的性质,1e18高的竹子进行不超过10次的魔法就可以到1,所以我就直接用栈进行存储,但是全mle了。。感觉空间复杂度没问题啊。求助qwq 代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=(2e5)+5;
stack<ll>h[maxn];
stack<ll>tmp;
ll ans=0;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		ll x;
		scanf("%lld",&x);
		while(x!=1){
			h[i].push(x);
			ans++;
			x=sqrt((x/2)+1);
		}
		h[i].push(x);
	}
	for(int i=1;i<n;i++){
		tmp=h[i+1];
		while(!tmp.empty()&&!h[i].empty()&&tmp.top()==h[i].top()){
			ans--;
			tmp.pop();
			h[i].pop();
		}
		ans++;
	}
	printf("%lld\n",ans);
	return 0;
}
2023/4/4 15:40
加载中...