当我保持较小的x时,我的代码在dp[105][x]时可以很好地处理小数字。但是当long long输入到来时,我需要将x更改为大于10^9的值,然后这个错误就出现在/tmp/cc35Ue0y.o: In function `__static_initialization_and_destruction_0(int, int)': main.cpp:(.text+0x294): relocation truncated to fit: R_X86_64_32 against `.bss' main.cpp:(.text+0x2a3): relocation truncated to fit: R_X86_64_32 against `.bss',如果我不更改,它就会给出分段错误。我的代码是为Atcode DP竞赛E问题https://atcoder.jp/contests/dp/tasks/dp_e
代码:
#include <bits/stdc++.h>
using namespace std;
int n, v[105];
long long int wt[105] , W, dp[105][105];
long long int fun(int n, long long int W, long long int wt[], int v[]) {
if(n == 0 || W == 0) return 0;
if(dp[n][W] != -1) return dp[n][W];
if(wt[n] <= W) {
return dp[n][W] = max(v[n] + fun(n-1, W - wt[n], wt, v), fun(n-1, W, wt, v));
}
else {
return dp[n][W] = fun(n-1, W, wt, v);
}
}
int main()
{
cin >> n >> W;
// for(int i=0;i<N;i++) {
// for(int j=0;j<2;j++) {
// cin >> a[i][j];
// }
// }
for(int i=1;i<=n;i++) {
cin >> wt[i] >> v[i];
}
memset(dp, -1, sizeof(dp));
cout << fun(n, W, wt, v);
return 0;
}我需要做哪些更改。
发布于 2020-12-08 17:49:26
堆栈通常是有限的,使用动态内存来处理,如std::vector
或auto dp = new std::array<std::array<long long int, 105>, 1'000'000'000>;
但是堆的大小似乎更大了.
你可能需要改变你的算法。
https://stackoverflow.com/questions/65196569
复制相似问题