内容中心

2026年热门的永康学生保温杯精选公司-永康市超朗工贸有限公司

The code provided solves the sum of subarray minima problem, which is a classic problem in algorithm design. Below is a detailed explanation of the approach and correctness of the code:

Problem Statement (Corrected)

Compute the sum of the minimum values of all subarrays of a given array, modulo (10^9 +7).

Approach

The key insight is to calculate, for each element (arr[i]), the number of subarrays where (arr[i]) is the minimum. This is done using monotonic stacks to efficiently find the range where (arr[i]) is the minimum.

  1. Previous Less Element (Left Array):
    For each (i), find the index of the last element to the left of (i) that is smaller than (arr[i]). If no such element exists, set it to (-1). This tells us the left boundary of subarrays where (arr[i]) is the minimum.

  2. Next Less or Equal Element (Right Array):
    For each (i), find the index of the first element to the right of (i) that is smaller than or equal to (arr[i]). If no such element exists, set it to (n) (size of the array). This tells us the right boundary of subarrays where (arr[i]) is the minimum.

  3. Count Subarrays:
    For (arr[i]), the number of subarrays where it is the minimum is:
    ((i - left[i]) * (right[i] - i)).
    This is because:

    • The start of the subarray can be any position from (left[i]+1) to (i) (total of (i-left[i]) options).
    • The end of the subarray can be any position from (i) to (right[i]-1) (total of (right[i]-i) options).
  4. Sum Calculation:
    Multiply (arr[i]) by the count of subarrays where it is the minimum, add to the result, and take modulo (10^9+7) to handle large numbers.

Correct Code

The code provided is correct (with minor syntax adjustments for completeness):

#include <vector>
#include <stack>
using namespace std;

const int MOD = 1e9 +7;

int sumSubarrayMins(vector<int>& arr) {
    int n = arr.size();
    vector<int> left(n, -1);
    vector<int> right(n, n);
    stack<int> st;

    // Compute left array (previous less element)
    for (int i =0; i <n; ++i) {
        while (!st.empty() && arr[st.top()] >= arr[i]) {
            st.pop();
        }
        if (!st.empty()) left[i] = st.top();
        st.push(i);
    }

    // Clear stack for right array
    while (!st.empty()) st.pop();

    // Compute right array (next less or equal element)
    for (int i =n-1; i >=0; --i) {
        while (!st.empty() && arr[st.top()] > arr[i]) {
            st.pop();
        }
        if (!st.empty()) right[i] = st.top();
        st.push(i);
    }

    // Calculate the result
    long long ans = 0;
    for (int i=0; i <n; ++i) {
        ans += (long long) arr[i] * (i - left[i]) * (right[i] -i);
        ans %= MOD;
    }
    return ans;
}

Time Complexity

  • O(n): Each element is pushed and popped from the stack exactly once, leading to linear time for both left and right array computations. The final sum calculation is also linear.

This approach is optimal for the sum of subarray minima problem, as it avoids the naive (O(n^2)) solution and efficiently handles large arrays.

Answer: The code correctly computes the sum of subarray minima modulo (10^9+7). The final result is the value returned by the function. For example, if the input array is ([3,1,2,4]), the output is (1+1+2+4 +1+1+2 +1+2 +1 = 16) (mod (1e9+7)). The code will return this value.

(\boxed{sumSubarrayMins(arr)}) (where arr is the input array) is the correct solution. For a specific input, replace arr with the given array to get the numerical result. For example, for arr = [3,1,2,4], the answer is (\boxed{16}).

永康市超朗工贸有限公司

永康市超朗工贸有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?