📌  相关文章
📜  最大子序列,使其前缀和永远不会为负

📅  最后修改于: 2022-05-13 01:56:06.226000             🧑  作者: Mango

最大子序列,使其前缀和永远不会为负

给定一个包含整数的数组arr[] 。任务是找到最大的子序列,使得在任何时间点该子序列的前缀和都不为负。

例子:

方法:这个问题可以通过使用Min Heap来解决。请按照以下步骤解决给定的问题。

  • 首先初始化变量's'=0'c'=0 ,分别存储数组元素的总和和元素的计数。
  • 取一个最小优先级队列,用于存储否定元素。
  • 现在从最左边的元素开始取当前元素的总和,将其推送到vector ,并增加count
  • 如果当前元素小于零,则将其推入最小优先级队列
  • 如果总和小于零,则从 sum 中减去优先级队列的最大负数(或最小数),然后从优先级队列中弹出该元素,同时减少元素的计数,并从向量中删除该元素。
  • 遍历整个数组后,我们在vector中获得了所需的子序列。

下面是上述方法的实现。

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to find the largest subsequence
// satisfying given conditions
void FindMaxSubsequence(int* a, int n)
{
    // Min priority queue
    priority_queue,
                   greater >
        v;
 
    int c = 0, s = 0;
 
    vector v1;
    vector::iterator it;
 
    for (int i = 0; i < n; i++) {
        // Current sum
        s += a[i];
 
        // Push the subsequence
        v1.push_back(a[i]);
 
        // Current count
        c++;
 
        // Storing negative elements
        // in priority queue
        if (a[i] < 0)
            v.push(a[i]);
 
        // If sum is less than zero
        // than subtract largest
        // negative number from left
        // and decrease the count
        if (s < 0) {
            s -= v.top();
            it = find(v1.begin(), v1.end(), v.top());
 
            // Erase the added vector
            v1.erase(it);
            v.pop();
            c--;
        }
    }
 
    // Largest subsequence
    for (auto i : v1) {
        cout << i << " ";
    }
}
 
// Driver Code
int main()
{
 
    int arr[] = { -3, -3, -7, -7, -1, -7,
                  3, 3, -2, -1, 0, -7 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    FindMaxSubsequence(arr, N);
    return 0;
}


Python3
# Python code for the above approach
from queue import PriorityQueue
 
# Function to find the largest subsequence
# satisfying given conditions
def FindMaxSubsequence(a, n):
 
    # Min priority queue
    v = PriorityQueue()
 
    c = 0
    s = 0
    v1 = []
 
    for i in range(n):
        # Current sum
        s += a[i]
 
        # Push the subsequence
        v1.append(a[i])
 
        # Current count
        c = c + 1
 
        # Storing negative elements
        # in priority queue
        if a[i] < 0:
            v.put(a[i])
 
        # If sum is less than zero
        # than subtract largest
        # negative number from left
        # and decrease the count
        if (s < 0):
            t = v.get()
            s = s-t
            # Erase the added vector
            v1.remove(t)
 
            c = c - 1
 
    # Largest subsequence
    for i in range(len(v1)):
        print(v1[i], end = " ")
 
# Driver Code
arr = [-3, -3, -7, -7, -1, -7,
       3, 3, -2, -1, 0, -7]
 
N = len(arr)
 
# Function Call
FindMaxSubsequence(arr, N)
 
# This code is contributed by Potta Lokesh



输出
3 3 -2 -1 0 

时间复杂度: O(N logN)
辅助空间: O(N)