📌  相关文章
📜  通过将 K 长度子数组中的所有元素增加 1 恰好 M 次来最大化最小数组元素

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

通过将 K 长度子数组中的所有元素增加 1 恰好 M 次来最大化最小数组元素

给定一个大小为N的数组arr[]以及整数MK ,任务是通过执行M个操作来找到最小数组元素的最大可能值。在每个操作中,将长度为K的连续子数组中所有元素的值增加1

例子:

方法:问题可以通过使用二分搜索来解决。遍历数组arr[]并为每个元素arr[i]计算所需的操作数。如果当前元素需要更新x次,则将x添加到答案中,并将长度为K的连续片段更新x次。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
#define ll long long
ll n, m, k, l, r, i;
 
// Function to check if the smallest
// value of v is achievable or not
ll check(ll v, vector& a)
{
    ll tec = 0, ans = 0;
 
    // Create array to
    // store previous moves
    vector b(n + k + 1);
 
    for (i = 0; i < n; i++) {
 
        // Remove previous moves
        tec -= b[i];
 
        if (a[i] + tec < v) {
 
            // Add balance to ans
            ll mov = v - a[i] - tec;
            ans = ans + mov;
 
            // Update contiguous
            // subarray of length k
            tec += mov;
            b[i + k] = mov;
        }
    }
 
    // Number of moves
    // should not exceed m
    return (ans <= m);
}
 
// Function to find the maximum
// value of the smallest array
// element that can be obtained
ll FindLargest(vector a)
{
    l = 1;
    r = pow(10, 10);
 
    // Perform Binary search
    while (r - l > 0) {
 
        ll tm = (l + r + 1) / 2;
 
        if (check(tm, a))
            l = tm;
        else
            r = tm - 1;
    }
    return l;
}
 
// Driver Code
int main()
{
    // Given Input
    vector a{ 2, 2, 2, 2, 1, 1 };
    m = 2;
    k = 3;
    n = a.size();
 
    // Function Call
    cout << FindLargest(a);
    return 0;
}


Java
// Java program for above approach
class GFG{
     
static long n, m, k, l, r, i;
 
// Function to check if the smallest
// value of v is achievable or not
static boolean check(long v, long[] a)
{
    long tec = 0, ans = 0;
     
    // Create array to
    // store previous moves
    long[] b = new long[(int)(n + k + 1)];
 
    for(int i = 0; i < n; i++)
    {
         
        // Remove previous moves
        tec -= b[i];
 
        if (a[i] + tec < v)
        {
             
            // Add balance to ans
            long mov = v - a[i] - tec;
            ans = ans + mov;
 
            // Update contiguous
            // subarray of length k
            tec += mov;
            b[i + (int)k] = mov;
        }
    }
 
    // Number of moves
    // should not exceed m
    return ans <= m;
}
 
// Function to find the maximum
// value of the smallest array
// element that can be obtained
static long FindLargest(long[] a)
{
    l = 1;
    r = (long)Math.pow(10, 10);
 
    // Perform Binary search
    while (r - l > 0)
    {
        long tm = (l + r + 1) / 2;
 
        if (check(tm, a))
            l = tm;
        else
            r = tm - 1;
    }
    return l;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Input
    long[] a = { 2, 2, 2, 2, 1, 1 };
    m = 2;
    k = 3;
    n = a.length;
 
    // Function Call
    System.out.println(FindLargest(a));
}
}
 
// This code is contributed by hritikrommie.


Python3
# Python 3 program for above approach
 
n = 0
m = 0
k = 0
l = 0
r = 0
i = 0
 
from math import pow
# Function to check if the smallest
# value of v is achievable or not
def check(v, a):
    tec = 0
    ans = 0
 
    # Create array to
    # store previous moves
    b = [0 for i in range(n + k + 1)]
 
    for i in range(n):
        # Remove previous moves
        tec -= b[i]
 
        if (a[i] + tec < v):
            # Add balance to ans
            mov = v - a[i] - tec
            ans = ans + mov
 
            # Update contiguous
            # subarray of length k
            tec += mov
            b[i + k] = mov
 
    # Number of moves
    # should not exceed m
    return (ans <= m)
 
# Function to find the maximum
# value of the smallest array
# element that can be obtained
def FindLargest(a):
    l = 1
    r = pow(10, 10)
 
    # Perform Binary search
    while (r - l > 0):
        tm = (l + r + 1) // 2
 
        if (check(tm, a)):
            l = tm
        else:
            r = tm - 1
    return l
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    a = [2, 2, 2, 2, 1, 1]
    m = 2
    k = 3
    n = len(a)
 
    # Function Call
    print(int(FindLargest(a)))
     
    # This code is contributed by ipg2016107.


C#
// C# program for above approach
using System;
 
public class GFG
{
     
static long n, m, k, l, r, i;
 
// Function to check if the smallest
// value of v is achievable or not
static bool check(long v, long[] a)
{
    long tec = 0, ans = 0;
     
    // Create array to
    // store previous moves
    long[] b = new long[(int)(n + k + 1)];
 
    for(int i = 0; i < n; i++)
    {
         
        // Remove previous moves
        tec -= b[i];
 
        if (a[i] + tec < v)
        {
             
            // Add balance to ans
            long mov = v - a[i] - tec;
            ans = ans + mov;
 
            // Update contiguous
            // subarray of length k
            tec += mov;
            b[i + (int)k] = mov;
        }
    }
 
    // Number of moves
    // should not exceed m
    return ans <= m;
}
 
// Function to find the maximum
// value of the smallest array
// element that can be obtained
static long FindLargest(long[] a)
{
    l = 1;
    r = (long)Math.Pow(10, 10);
 
    // Perform Binary search
    while (r - l > 0)
    {
        long tm = (l + r + 1) / 2;
 
        if (check(tm, a))
            l = tm;
        else
            r = tm - 1;
    }
    return l;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given Input
    long[] a = { 2, 2, 2, 2, 1, 1 };
    m = 2;
    k = 3;
    n = a.Length;
 
    // Function Call
    Console.WriteLine(FindLargest(a));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
2

时间复杂度: O(NlogN)
辅助空间: O(N + K)