给定一个大小为N的数组arr[] ,任务是将点分配给每个不同的数组元素,以便分配的点总和尽可能最小。
分配积分需要满足以下条件:
- 可以赋予数字的最小点数是 1。
- 每个不同的元素都应该分配一个唯一的值。
- 应根据数字的值分配分数,即与较大的数字相比,较小的数字应分配较低的分数。
例子:
Input: N = 5, arr[] = {10, 20, 10, 25}
Output: 7
Explanation: Assign 1 point to 10, 2 points to 20, 3 points to 25. Sum of points = 1 + 2 + 1 + 3 = 7, which is the minimum possible.
Input: N = 4, arr[] = {7, 7, 7, 7}
Output: 4
Explanation: Assign 1 point to 7. Sum of points = 1 + 1 + 1 + 1 = 4, which is the minimum possible.
方法:可以通过将较低的点分配给较小的元素来贪婪地解决该问题。请按照以下步骤解决问题:
- 将变量ans初始化为1以存储所需的结果。
- 按递增顺序对给定数组进行排序。
- 初始化一个变量point为1来存储当前元素的点数。
- 使用变量i在范围[0, N-1] 中迭代
- 如果arr[i]等于arr[i+1] ,则按point递增ans 。
- 否则将point的值增加1 ,然后将point添加到ans 。
- 打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to assign points to each
// number in an array such that the
// sum of points assigned is minimized
void findMinimumSum(int arr[], int n)
{
// Sort the array in increasing order
sort(arr, arr + n);
// Assign point_value to 1
int point_value = 1;
// Store the required result
int total_points = 1;
// Traverse the array, arr[]
for (int i = 0; i < n - 1; i++) {
// If adjacent elements are same add
// current point_value to total_point
if (arr[i] == arr[i + 1])
total_points += point_value;
// Otherwise, assign a new point_value
// and add it to total_points
else {
point_value = point_value + 1;
total_points += point_value;
}
}
// Print the result
cout << total_points;
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 10, 20, 10, 25 };
int n = 4;
// Function Call
findMinimumSum(arr, n);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to assign points to each
// number in an array such that the
// sum of points assigned is minimized
static void findMinimumSum(int []arr, int n)
{
// Sort the array in increasing order
Arrays.sort(arr);
// Assign point_value to 1
int point_value = 1;
// Store the required result
int total_points = 1;
// Traverse the array, arr[]
for(int i = 0; i < n - 1; i++)
{
// If adjacent elements are same add
// current point_value to total_point
if (arr[i] == arr[i + 1])
total_points += point_value;
// Otherwise, assign a new point_value
// and add it to total_points
else
{
point_value = point_value + 1;
total_points += point_value;
}
}
// Print the result
System.out.print(total_points);
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int []arr = { 10, 20, 10, 25 };
int n = 4;
// Function Call
findMinimumSum(arr, n);
}
}
// This code is contributed by sanjoy_62.
Python3
# Python3 program for the above approach
# Function to assign points to each
# number in an array such that the
# sum of points assigned is minimized
def findMinimumSum(arr, n):
# Sort the array in increasing order
arr = sorted(arr)
# Assign point_value to 1
point_value = 1
# Store the required result
total_points = 1
# Traverse the array, arr[]
for i in range(n - 1):
# If adjacent elements are same add
# current point_value to total_point
if (arr[i] == arr[i + 1]):
total_points += point_value
# Otherwise, assign a new point_value
# and add it to total_points
else:
point_value = point_value + 1
total_points += point_value
# Print the result
print(total_points)
# Driver Code
if __name__ == '__main__':
# Given Input
arr = [ 10, 20, 10, 25 ]
n = 4
# Function Call
findMinimumSum(arr, n)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to assign points to each
// number in an array such that the
// sum of points assigned is minimized
static void findMinimumSum(int []arr, int n)
{
// Sort the array in increasing order
Array.Sort(arr);
// Assign point_value to 1
int point_value = 1;
// Store the required result
int total_points = 1;
// Traverse the array, arr[]
for(int i = 0; i < n - 1; i++)
{
// If adjacent elements are same add
// current point_value to total_point
if (arr[i] == arr[i + 1])
total_points += point_value;
// Otherwise, assign a new point_value
// and add it to total_points
else
{
point_value = point_value + 1;
total_points += point_value;
}
}
// Print the result
Console.Write(total_points);
}
// Driver Code
public static void Main()
{
// Given Input
int []arr = { 10, 20, 10, 25 };
int n = 4;
// Function Call
findMinimumSum(arr, n);
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
输出:
7
时间复杂度: O(N*logN)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。