I am solving a problem for which, I need to calculate the prefix and suffix sum values. When I do it this way:
class Solution {
public:
int minimumAverageDifference(vector<int>& nums) {
long n=size(nums);
vector<long long> left(n,0ll), right(n,0ll);
partial_sum(begin(nums), end(nums), begin(left));
partial_sum(rbegin(nums), rend(nums), rbegin(right));
return 0;
}
};
This works fine for smaller input values, but when the input is very large, I get an error:
Line 258: Char 43: runtime error: signed integer overflow: 2147453785 + 36049 cannot be represented in type 'int' (stl_numeric.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_numeric.h:267:43
However, the traditional for-loop works just fine for all the inputs, including the very large ones:
class Solution {
public:
int minimumAverageDifference(vector<int>& nums) {
long n=size(nums);
vector<long long> left(n,0ll), right(n,0ll);
left[0]=nums[0];
for(int i=1; i<n; i++) {
left[i]=left[i-1]+nums[i];
}
right[n-1]=nums[n-1];
for(int i=n-2; i>=0; i--) {
right[i]=right[i+1]+nums[i];
}
return 0;
}
};
What am I missing about the usage of partial_sum()
?
1条答案
按热度按时间djmepvbi1#
std::partial_sum()
是defined,使得累加器类型是作为输入范围元素的类型的:对于采用自定义二进制运算的重载也是如此。没有简单的方法来覆盖该类型--你必须以某种方式修改输入范围本身。
如果你真的想使用
std::partial_sum()
,你可以将输入范围复制到std::vector<long long>
,或者使用boost::transform_iterator
动态转换它:Demo
但是,最简单的解决方案是使用
std::inclusive_scan()
,它接受确定累加器类型的初始值:Demo