📜  数组所有后缀的分解值之和

📅  最后修改于: 2021-06-26 11:19:42             🧑  作者: Mango

给定一个数组arr [] ,任务是找到后缀子数组的分解值之和。

分解值:子数组的分解值是子数组中可能的分区计数。数组在索引处的分区i仅当数组中的元素小于当前索引时才可以执行。即A [k]

例子:

方法:想法是使用Stack来解决此问题。下面是该方法的说明

  • 从头到尾遍历数组。
  • 保持最小变量并回答变量。
  • 如果堆栈为空或当前元素小于堆栈顶部–
    • 将S [i]压入堆栈。
    • 将答案增加堆栈的大小。
    • 此外,请保持最小值到现在。
  • 除此以外,
    • 只要堆栈的顶部小于当前元素,就继续弹出这些块。
    • 到现在为止,使用当前元素更新最小值。
    • 将最小值压入堆栈。因为,我们希望子数组的最小值表示该子数组
    • 将答案增加堆栈的大小。

下面是上述方法的实现:

C++
// C++ implementation to find the 
// sum of Decomposition values of 
// all suffixes of an array
  
#include 
using namespace std;
#define int long long int
  
// Function to find the decomposition 
// values of the array 
int decompose(vector S)
{
    // Stack
    stack s;
    int N = S.size();
    int ans = 0;
      
    // Variable to maintain 
    // min value in stack
    int nix = INT_MAX;
      
    // Loop to iterate over the array
    for (int i = N - 1; i >= 0; i--) {
          
        // Condition to check if the
        // stack is empty
        if (s.empty()) {
            s.push(S[i]);
            nix = S[i];
        }
        else {
              
            // Condition to check if the 
            // top of the stack is greater
            // than the current element
            if (S[i] < s.top()) {
                s.push(S[i]);
                nix = min(nix, S[i]);
            }
            else {
                int val = S[i];
                  
                // Loop to pop the element out
                while (!s.empty() &&
                       val >= s.top()) {
                    s.pop();
                }
                nix = min(nix, S[i]);
                s.push(nix);
            }
        }
          
        // the size of the stack is the 
        // max no of subarrays for 
        // suffix till index i
        // from the right
        ans += s.size();
    }
  
    return ans;
}
  
// Driver Code
signed main()
{
    vector S = { 9, 6, 9, 35 };
    cout << decompose(S) << endl;
    return 0;
}


Java
// Java implementation to find the 
// sum of Decomposition values of 
// all suffixes of an array
import java.util.*;
  
class GFG{
  
// Function to find the decomposition 
// values of the array 
static int decompose(Vector S)
{
      
    // Stack
    Stack s = new Stack();
    int N = S.size();
    int ans = 0;
      
    // Variable to maintain 
    // min value in stack
    int nix = Integer.MAX_VALUE;
      
    // Loop to iterate over the array
    for(int i = N - 1; i >= 0; i--)
    {
          
       // Condition to check if the
       // stack is empty
       if (s.isEmpty())
       {
           s.add(S.get(i));
           nix = S.get(i);
       }
       else 
       {
             
           // Condition to check if the 
           // top of the stack is greater
           // than the current element
           if (S.get(i) < s.peek()) 
           {
               s.add(S.get(i));
               nix = Math.min(nix, S.get(i));
           }
           else
           {
               int val = S.get(i);
                 
               // Loop to pop the element out
               while (!s.isEmpty() && val >= s.peek())
               {
                   s.pop();
               }
               nix = Math.min(nix, S.get(i));
               s.add(nix);
           }
       }
         
       // The size of the stack is the 
       // max no of subarrays for 
       // suffix till index i
       // from the right
       ans += s.size();
    }
    return ans;
}
  
// Driver Code
public static void main(String args[])
{
    Vector S = new Vector();
    S.add(9);
    S.add(6);
    S.add(9);
    S.add(35);
      
    System.out.println(decompose(S));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to find the 
# sum of Decomposition values of 
# all suffixes of an array
import sys
  
# Function to find the decomposition 
# values of the array 
def decompose(S):
  
    # Stack
    s = []
    N = len(S)
    ans = 0
      
    # Variable to maintain 
    # min value in stack
    nix = sys.maxsize
      
    # Loop to iterate over the array
    for i in range(N - 1, -1, -1):
          
        # Condition to check if the
        # stack is empty
        if (len(s) == 0):
            s.append(S[i])
            nix = S[i]
          
        else:
              
            # Condition to check if the 
            # top of the stack is greater
            # than the current element
            if (S[i] < s[-1]):
                s.append(S[i])
                nix = min(nix, S[i])
              
            else:
                val = S[i]
                  
                # Loop to pop the element out
                while (len(s) != 0 and
                          val >= s[-1]):
                    s.pop()
              
                nix = min(nix, S[i]);
                s.append(nix)
          
        # The size of the stack is the 
        # max no of subarrays for 
        # suffix till index i
        # from the right
        ans += len(s)
  
    return ans
  
# Driver Code
if __name__ =="__main__":
      
    S = [ 9, 6, 9, 35 ]
      
    print(decompose(S))
  
# This code is contributed by chitranayal


C#
// C# implementation to find the 
// sum of Decomposition values of 
// all suffixes of an array 
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to find the decomposition 
// values of the array
static int decompose(List S) 
{ 
      
    // Stack 
    Stack s = new Stack(); 
      
    int N = S.Count; 
    int ans = 0; 
      
    // Variable to maintain 
    // min value in stack 
    int nix = Int32.MaxValue; 
      
    // Loop to iterate over the array 
    for(int i = N - 1; i >= 0; i--) 
    { 
          
        // Condition to check if the 
        // stack is empty 
        if (s.Count == 0) 
        { 
            s.Push(S[i]); 
            nix = S[i]; 
        } 
        else
        { 
              
            // Condition to check if the 
            // top of the stack is greater 
            // than the current element 
            if (S[i] < s.Peek()) 
            { 
                s.Push(S[i]); 
                nix = Math.Min(nix, S[i]); 
            } 
            else
            { 
                int val = S[i]; 
                      
                // Loop to pop the element out 
                while (s.Count != 0 && val >= s.Peek()) 
                { 
                    s.Pop(); 
                } 
                nix = Math.Min(nix, S[i]); 
                s.Push(nix); 
            } 
        } 
          
        // The size of the stack is the 
        // max no of subarrays for 
        // suffix till index i 
        // from the right 
        ans += s.Count; 
    } 
    return ans; 
}
  
// Driver code
static void Main() 
{
    List S = new List();
    S.Add(9); 
    S.Add(6); 
    S.Add(9); 
    S.Add(35); 
  
    Console.WriteLine(decompose(S));
}
}
  
// This code is contributed by divyeshrabadiya07


输出:
8

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。