重量总和约为10^9的背包
Knapsack with sum of weights of about 10^9
本文关键字:背包 更新时间:2023-10-16
几周
前我在一次编程比赛中遇到了一个问题,这个问题可以简化为背包0/1问题。
但我做不到,因为最大权重约为 10^9,所以在 c++ 中我不能使用数组。虽然项目数量约为 10^5。
我能想到的解决这个问题的一种方法是使用 STL 映射,但不确定如何做到这一点。
任何帮助将不胜感激。谢谢。
如果你只需要一个巨大的数组,为什么不把它分解成更小的数组呢?
class Huge_array{
public:
Huge_array(size_t max_size):
max_size(max_size),
store(max_size/v_size, vector<int>(v_size))
{}
int& operator[](size_t index)
{
assert(0<=index && index<max_size);
return store[index/v_size][index%v_size];
}
private:
size_t max_size;
const int v_size=100000;
vector<vector<int> > store;
};
我希望它没有任何错别字。
用法:
Huge_array my_array((size_t)10e9);
my_array[30000000]=10; // and so on upto size_t(10e9) - 1
如果您需要更大的价值值,则可以将vector<int>
vector<long>
或vector<double>
或其他任何Huge_array
。
相关文章:
- 0-1背包代码中的错误.我的代码中有什么错误
- 分数背包边缘箱
- 具有多种约束(例如重量、体积等)的背包
- 背包问题,视觉工作室问题
- 分段故障背包问题
- 0-1 大重量背包
- 具有最小值和最大值的多维背包
- 此背包代码显示浮点[float]无效类型错误.可能是什么原因
- 背包问题根据单位重量的利润对数组进行排序
- Python到C++:使用递归列出背包的所有组合的算法
- 蛮力背包的C 一部分
- 重量总和约为10^9的背包
- 可以使用 1D 数组实现 0-1 背包吗?
- 有没有解决方案而不是在C++中移动背包回溯算法
- 回溯背包实施与C++
- 背包 - 如何识别使用的重量
- 背包的变体
- 使用动态规划的值无关背包问题
- 先例制约双背包
- 具有双重数据类型的背包