如何估算STL所使用的内存
  • 板块学术版
  • 楼主Escapism
  • 当前回复17
  • 已保存回复17
  • 发布时间2023/8/5 13:47
  • 上次更新2023/11/3 05:46:17
查看原帖
如何估算STL所使用的内存
361505
Escapism楼主2023/8/5 13:47

RT,今天一份广搜代码由于Queue + vector的原因全部MLE。

所以想问一下如何有效的估算STL所使用的内存。

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

const int MAXN = 5 * 1e5 + 5; //这个地方开 1000 也会挂,所以应该不是定长数组的问题
int n,m,k;
struct Edge{
	int to,val,en;
};
vector<Edge> G[MAXN];
int a[MAXN],ans = 0x3f3f3f3f;
int s[MAXN];
struct Node{
	int point,sum1,sum2;
};
queue<Node> Q;

void Solve(){
	while(!Q.empty()){
		Node tmp = Q.front();
		Q.pop();
		int p = tmp.point,s1 = tmp.sum1,s2 = tmp.sum2;
		if (s1 > s[p]) continue;
		if (p == n){
			ans = min(ans,s1);
			continue;
		}
		int x,y;
		for (int i = 0;i < G[p].size();i++){
			x = s1 + G[p][i].val,y = s2 + G[p][i].en;
			if (G[p][i].en == 1 && y > k) continue;
			if (x > s[G[p][i].to]) continue;
			else{
				if (x < s[G[p][i].to]) s[G[p][i].to] = x;
				Q.push(Node{G[p][i].to,x,y});
			}
		}
	}
}
int main(){
	//freopen("step.in","r",stdin);
	//freopen("step.out","w",stdout);
	scanf("%d%d%d",&n,&m,&k);
	for (int i = 1;i <= MAXN;i++) s[i] = 0x3f3f3f3f;
	for (int i = 1;i <= n;i++){
		scanf("%d",&a[i]);
		if (a[i] != 0 && a[i] != i) G[i].push_back(Edge{a[i],0,1});
	}
	for (int i = 1;i <= n;i++){
		for (int j = max(i - m,1);j <= min(i + m,n);j++) G[i].push_back(Edge{j,1,0});
	}
	s[1] = 0;
	Q.push(Node{1,0,0});
	Solve();
	cout<<ans<<endl;
	return 0;
}

2023/8/5 13:47
加载中...