📌  相关文章
📜  在线性时间的 k 次操作中最大化数组最右边的元素

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

在线性时间的 k 次操作中最大化数组最右边的元素

给定一个大小为N的数组arr[ ]和一个整数p ,任务是找到数组arr[ ]最右边元素最大可能值,最多执行k 次操作。在一次操作中,将arr[i]减少p并增加arr[ i+1]p

例子:

天真方法:这个问题可以使用贪心方法天真地解决。请按照以下步骤解决问题:

  • 运行一个while 循环,直到k大于 0。
  • while循环内运行for 循环, for i在范围[N-2, 0] 内
  • 检查arr[i]是否大于1 ,将arr[i+1]增加p并将arr[i]减少p中断 for loop
  • k的值减少 1。
  • 打印arr[N-1]

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to calculate maximum value of
// Rightmost element
void maxRightmostElement(int N, int k, int p, int arr[]){
   
     // Calculating maximum value of
     // Rightmost element
     while(k)
         {
        
          for(int i=N-2;i>=0;i--)
              {
             
                // Checking if arr[i] is operationable
                if(arr[i]>= p)
                  {
                      // Performing operation of i-th element
                      arr[i]=arr[i]-p;
                   
                      arr[i+1]=arr[i+1]+p;
                   
                      break;
                   
                  } 
             
              }
        
         // Decreasing the value of k by 1
         k--;
        }
   
        // Printing rightmost element
        cout<


Java
// Java program for the above approach
import java.io.*;
class GFG {
 
    // Function to calculate maximum value of
    // Rightmost element
    static int maxRightmostElement(int N, int k, int p,
                                   int arr[])
    {
       
        // Calculating maximum value of
        // Rightmost element
        while (k > 0) {
 
            for (int i = N - 2; i >= 0; i--) {
 
                // Checking if arr[i] is operationable
                if (arr[i] >= p)
                {
                   
                    // Performing operation of i-th element
                    arr[i] = arr[i] - p;
 
                    arr[i + 1] = arr[i + 1] + p;
 
                    break;
                }
            }
 
            // Decreasing the value of k by 1
            k--;
        }
 
        // returning the rightmost element
        return arr[N - 1];
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Input
        int N = 4, k = 5, p = 2;
        int arr[] = { 3, 8, 1, 4 };
       
        // Function Call
        System.out.println(
            maxRightmostElement(N, k, p, arr));
    }
}
 
// This code is contributed by dwivediyash


Python3
# Python program for the above approach
 
# Function to calculate maximum value of
# Rightmost element
def maxRightmostElement(N, k, p, arr):
   
    # Calculating maximum value of
    # Rightmost element
    while(k) :
        
        for i in range(N - 2, -1, -1) :
             
            # Checking if arr[i] is operationable
            if(arr[i] >= p) :
                 
                # Performing operation of i-th element
                arr[i] = arr[i] - p
                   
                arr[i + 1] = arr[i + 1] + p
                   
                break
                   
        # Decreasing the value of k by 1
        k = k - 1
         
    # Printing rightmost element
    print(arr[N-1])
 
 
# Driver Code
 
# Given Input
N = 4
p = 2
k = 5
arr = [3, 8, 1, 4]
   
# Function Call
maxRightmostElement(N, k, p, arr)
 
# This code is contributed by sanjoy_62.


C#
using System;
 
public class GFG {
 
    // Function to calculate maximum value of
    // Rightmost element
    static int maxRightmostElement(int N, int k, int p,
                                   int []arr)
    {
 
        // Calculating maximum value of
        // Rightmost element
        while (k > 0) {
 
            for (int i = N - 2; i >= 0; i--) {
 
                // Checking if arr[i] is operationable
                if (arr[i] >= p) {
 
                    // Performing operation of i-th element
                    arr[i] = arr[i] - p;
 
                    arr[i + 1] = arr[i + 1] + p;
 
                    break;
                }
            }
 
            // Decreasing the value of k by 1
            k--;
        }
 
        // returning the rightmost element
        return arr[N - 1];
    }
 
    // Driver Code
    static public void Main()
    {
       
        // Given Input
        int N = 4, k = 5, p = 2;
        int[] arr = { 3, 8, 1, 4 };
 
        // Function Call
        Console.WriteLine(
            maxRightmostElement(N, k, p, arr));
    }
}
 
// This code is contributed by maddler.


Javascript


C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to calculate maximum value of
// Rightmost element
void maxRightmostElement(int N, int k, int arr[]){
    
     // Initializing ans to store
     // Maximum valued rightmost element
     int ans = arr[N-1];
     
     // Calculating maximum value of
     // Rightmost element
     for(int i=N-2; i>=0; i--)
       {
        int d = min(arr[i]/2, k/(N-1-i));
        
        k-=d*(N-1-i);
        
        ans+=d*2;
        }
 
        // Printing rightmost element
        cout<


Java
// Java program for the above approach
import java.io.*;
class GFG {
 
  // Function to calculate maximum value of
  // Rightmost element
  static int maxRightmostElement(int N, int k, int p, int arr[])
  {
     
    // Initializing ans to store
    // Maximum valued rightmost element
    int ans = arr[N - 1];
 
    // Calculating maximum value of
    // Rightmost element
    for (int i = N - 2; i >= 0; i--) {
      int d = Math.min(arr[i] / p, k / (N - 1 - i));
 
      k -= d * (N - 1 - i);
 
      ans += d * p;
    }
 
    // returning rightmost element
    return  ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    // Given Input
    int N = 4, k = 5, p = 2;
    int arr[] = { 3, 8, 1, 4 };
    // Function Call
    System.out.println(maxRightmostElement(N, k, p, arr));
  }
}
 
// This code is contributed by dwivediyash


Python3
# Python 3 program for above approach
 
# Function to calculate maximum value of
# Rightmost element
def maxRightmostElement(N, k, arr):
   
    # Initializing ans to store
    # Maximum valued rightmost element
    ans = arr[N-1]
     
     # Calculating maximum value of
     # Rightmost element
    i = N - 2
    while(i >= 0):
        d = min(arr[i]//2, k//(N-1-i))
        
        k -= d * (N - 1 - i)
        
        ans+=d*2
 
        # Printing rightmost element
        i -= 1
    print(ans,end = " ")
      
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    N = 4
    k = 5
    arr = [3, 8, 1, 4]
   
    # Function Call
    maxRightmostElement(N, k, arr)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
public class GFG {
 
  // Function to calculate maximum value of
  // Rightmost element
  static int maxRightmostElement(int N, int k, int p, int []arr)
  {
     
    // Initializing ans to store
    // Maximum valued rightmost element
    int ans = arr[N - 1];
 
    // Calculating maximum value of
    // Rightmost element
    for (int i = N - 2; i >= 0; i--) {
      int d = Math.Min(arr[i] / p, k / (N - 1 - i));
 
      k -= d * (N - 1 - i);
 
      ans += d * p;
    }
 
    // returning rightmost element
    return  ans;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
     
    // Given Input
    int N = 4, k = 5, p = 2;
    int []arr = { 3, 8, 1, 4 };
     
    // Function Call
    Console.WriteLine(maxRightmostElement(N, k, p, arr));
  }
}
 
// This code is contributed by AnkThon


Javascript


输出
8

时间复杂度: O(N*k)
辅助空间: O(1)

高效的方法:上述方法可以通过观察这样一个事实来优化:数组arr[]的第 i 个元素可以在 N-1-i 次操作中对元素arr[N-1]贡献2 。请按照以下步骤解决问题:

  • 迭代一个循环,对于范围[N-2, 0] 中的i
  • ans初始化为arr[N-1]以存储最右边元素的最大值
  • 找到第 i 个元素的最小贡献,例如dd = min(a[i]/2, k/(N-1-i))。
  • k减少d*(N-1-i)并将ans增加d*2。
  • 打印ans,这将是最后一步

C++

// C++ program for above approach
#include 
using namespace std;
 
// Function to calculate maximum value of
// Rightmost element
void maxRightmostElement(int N, int k, int arr[]){
    
     // Initializing ans to store
     // Maximum valued rightmost element
     int ans = arr[N-1];
     
     // Calculating maximum value of
     // Rightmost element
     for(int i=N-2; i>=0; i--)
       {
        int d = min(arr[i]/2, k/(N-1-i));
        
        k-=d*(N-1-i);
        
        ans+=d*2;
        }
 
        // Printing rightmost element
        cout<

Java

// Java program for the above approach
import java.io.*;
class GFG {
 
  // Function to calculate maximum value of
  // Rightmost element
  static int maxRightmostElement(int N, int k, int p, int arr[])
  {
     
    // Initializing ans to store
    // Maximum valued rightmost element
    int ans = arr[N - 1];
 
    // Calculating maximum value of
    // Rightmost element
    for (int i = N - 2; i >= 0; i--) {
      int d = Math.min(arr[i] / p, k / (N - 1 - i));
 
      k -= d * (N - 1 - i);
 
      ans += d * p;
    }
 
    // returning rightmost element
    return  ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    // Given Input
    int N = 4, k = 5, p = 2;
    int arr[] = { 3, 8, 1, 4 };
    // Function Call
    System.out.println(maxRightmostElement(N, k, p, arr));
  }
}
 
// This code is contributed by dwivediyash

蟒蛇3

# Python 3 program for above approach
 
# Function to calculate maximum value of
# Rightmost element
def maxRightmostElement(N, k, arr):
   
    # Initializing ans to store
    # Maximum valued rightmost element
    ans = arr[N-1]
     
     # Calculating maximum value of
     # Rightmost element
    i = N - 2
    while(i >= 0):
        d = min(arr[i]//2, k//(N-1-i))
        
        k -= d * (N - 1 - i)
        
        ans+=d*2
 
        # Printing rightmost element
        i -= 1
    print(ans,end = " ")
      
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    N = 4
    k = 5
    arr = [3, 8, 1, 4]
   
    # Function Call
    maxRightmostElement(N, k, arr)
     
    # This code is contributed by SURENDRA_GANGWAR.

C#

// C# program for the above approach
using System;
 
public class GFG {
 
  // Function to calculate maximum value of
  // Rightmost element
  static int maxRightmostElement(int N, int k, int p, int []arr)
  {
     
    // Initializing ans to store
    // Maximum valued rightmost element
    int ans = arr[N - 1];
 
    // Calculating maximum value of
    // Rightmost element
    for (int i = N - 2; i >= 0; i--) {
      int d = Math.Min(arr[i] / p, k / (N - 1 - i));
 
      k -= d * (N - 1 - i);
 
      ans += d * p;
    }
 
    // returning rightmost element
    return  ans;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
     
    // Given Input
    int N = 4, k = 5, p = 2;
    int []arr = { 3, 8, 1, 4 };
     
    // Function Call
    Console.WriteLine(maxRightmostElement(N, k, p, arr));
  }
}
 
// This code is contributed by AnkThon

Javascript


输出
8

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