📌  相关文章
📜  每个元素除以10时,将数组总和最大化,以X为增量

📅  最后修改于: 2021-05-17 20:56:08             🧑  作者: Mango

给定由N个非负元素和整数X组成的数组arr [] ,任务是使X递增,以使每个元素除以10时数组的值之和,即

\sum\limits^{N-1}_{i=0} \lfloor arr_i/10 \rfloor

被最大化。打印最大值

\sum\limits^{N-1}_{i=0} \lfloor arr_i/10 \rfloor

可能的。
注意:任何元素的值都不能超过1000。
例子:

方法:

  1. 对于所有元素,计算将数字增加到10的下一个倍数所需的增量数,并将这些值存储在数组中,例如V。
  2. 计算元素可以增加10并保持其值<= 1000的最大次数,并将此值添加到变量中,例如,将其初始化为0的增量
  3. 对数组V排序以使其不变。
  4. 然后针对V中的每个值,执行所需的移动,并将某个元素增加到10的下一个倍数,这将答案增加1。
  5. 在执行总移动的同时,不要超过X。
  6. 在经历了V的所有元素之后,如果还剩下一些移动,则将其加到最小增量(剩余移动)/ 10之间的答案最小值中。

下面是上述方法的实现:

C++
// C++ program for the above problem
 
#include 
using namespace std;
 
void maximizeval10(int a[],
                   int n, int k)
{
    // initialize variables
    int increments = 0;
    int ans = 0;
    vector v;
 
    for (int i = 0; i < n; i++) {
 
        // add the current
        // contribution of the
        // element to the answer
        ans += (a[i] / 10);
 
        // if the value is
        // already maximum
        // then we can't change it
        if (a[i] == 1000)
            continue;
 
        else {
            // moves required to move
            // to the next multiple
            // of 10
            v.push_back(10 - a[i] % 10);
 
            // no of times we can
            // add 10 to this value
            // so that its value
            // does not exceed 1000.
            increments += (100
                           - ((a[i]) / 10)
                           - 1);
        }
    }
 
    // sort the array
    sort(v.begin(), v.end());
 
    int sum = 0;
 
    for (int i = 0; i < v.size();
         i++) {
 
        // adding the values to
        // increase the numbers
        // to the next multiple of 10
        sum += v[i];
        if (sum <= k) {
 
            // if the total moves
            // are less than X then
            // increase the answer
            ans++;
        }
        else
 
            // if the moves exceed
            // X then we cannot
            // increase numbers
            break;
    }
 
    // if there still remain
    // some moves
    if (sum < k) {
 
        // remaining moves
        int remaining = k - sum;
 
        // add minimim of increments and
        // remaining/10 to the
        // answer
        ans += min(increments,
                   remaining / 10);
    }
 
    // output the final answer
    cout << ans;
}
 
// Driver Code
int main()
{
    int N = 4;
    int X = 6;
 
    int A[N] = { 4, 8, 8, 8 };
    maximizeval10(A, N, X);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
public static void maximizeval10(int[] a, int n,
                                 int k)
{
     
    // Initialize variables
    int increments = 0;
    int ans = 0;
    Vector v = new Vector<>();
 
    for(int i = 0; i < n; i++)
    {
 
        // Add the current
        // contribution of the
        // element to the answer
        ans += (a[i] / 10);
 
        // If the value is
        // already maximum
        // then we can't change it
        if (a[i] == 1000)
            continue;
 
        else
        {
             
            // Moves required to move
            // to the next multiple
            // of 10
            v.add(10 - a[i] % 10);
 
            // No of times we can
            // add 10 to this value
            // so that its value
            // does not exceed 1000.
            increments += (100 - ((a[i]) /
                           10) - 1);
        }
    }
     
    // Sort the array
    Collections.sort(v);
 
    int sum = 0;
 
    for(int i = 0; i < v.size(); i++)
    {
         
        // Adding the values to
        // increase the numbers
        // to the next multiple of 10
        sum += v.get(i);
        if (sum <= k)
        {
             
            // If the total moves
            // are less than X then
            // increase the answer
            ans++;
        }
        else
 
            // If the moves exceed
            // X then we cannot
            // increase numbers
            break;
    }
 
    // If there still remain
    // some moves
    if (sum < k)
    {
         
        // Remaining moves
        int remaining = k - sum;
 
        // Add minimim of increments and
        // remaining/10 to the
        // answer
        ans += Math.min(increments,
                        remaining / 10);
    }
     
    // Output the final answer
    System.out.print(ans);
}
 
// Driver code
public static void main(String[] args)
{
    int N = 4;
    int X = 6;
    int A[] = { 4, 8, 8, 8 };
     
    maximizeval10(A, N, X);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above problem
def maximizeval10(a, n, k):
     
    # Initialize variables
    increments = 0
    ans = 0
    v = []
 
    for i in range (n):
 
        # Add the current
        # contribution of the
        # element to the answer
        ans += (a[i] // 10)
 
        # If the value is already
        # maximum then we can't
        # change it
        if (a[i] == 1000):
            continue
        else:
             
            # Moves required to move
            # to the next multiple
            # of 10
            v.append(10 - a[i] % 10)
 
            # No of times we can
            # add 10 to this value
            # so that its value
            # does not exceed 1000.
            increments += (100 - ((a[i]) //
                                     10) - 1);
 
    # Sort the array
    v.sort()
 
    sum = 0
    for i in range(len(v)):
 
        # Adding the values to
        # increase the numbers
        # to the next multiple of 10
        sum += v[i]
        if (sum <= k):
 
            # If the total moves
            # are less than X then
            # increase the answer
            ans += 1
         
        else:
 
            # If the moves exceed
            # X then we cannot
            # increase numbers
            break
 
    # If there still remain
    # some moves
    if (sum < k):
 
        # Remaining moves
        remaining = k - sum
 
        # Add minimim of increments
        # and remaining/10 to the
        # answer
        ans += min(increments,
                   remaining // 10)
 
    # Output the final answer
    print(ans)
 
# Driver Code
if __name__ =="__main__":
 
    N = 4
    X = 6
    A = [ 4, 8, 8, 8 ]
     
    maximizeval10(A, N, X)
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
     
public static void maximizeval10(int[] a,
                                 int n,
                                 int k)
{
  // Initialize variables
  int increments = 0;
  int ans = 0;
  List v = new List();
 
  for(int i = 0; i < n; i++)
  {
    // Add the current
    // contribution of the
    // element to the answer
    ans += (a[i] / 10);
 
    // If the value is
    // already maximum
    // then we can't change it
    if (a[i] == 1000)
      continue;
 
    else
    {
      // Moves required to move
      // to the next multiple
      // of 10
      v.Add(10 - a[i] % 10);
 
      // No of times we can
      // add 10 to this value
      // so that its value
      // does not exceed 1000.
      increments += (100 - ((a[i]) /
                     10) - 1);
    }
  }
 
  // Sort the array
  v.Sort();
 
  int sum = 0;
 
  for(int i = 0; i < v.Count; i++)
  {
    // Adding the values to
    // increase the numbers
    // to the next multiple of 10
    sum += v[i];
    if (sum <= k)
    {
      // If the total moves
      // are less than X then
      // increase the answer
      ans++;
    }
    else
 
      // If the moves exceed
      // X then we cannot
      // increase numbers
      break;
  }
 
  // If there still remain
  // some moves
  if (sum < k)
  {
    // Remaining moves
    int remaining = k - sum;
 
    // Add minimim of increments and
    // remaining/10 to the
    // answer
    ans += Math.Min(increments,
                    remaining / 10);
  }
 
  // Output the readonly answer
  Console.Write(ans);
}
 
// Driver code
public static void Main(String[] args)
{
  int N = 4;
  int X = 6;
  int []A = {4, 8, 8, 8};
  maximizeval10(A, N, X);
}
}
 
// This code is contributed by shikhasingrajput


输出:
3


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