给定的阵列ARR []由N个整数(基于1的索引)和两个整数M和S的,任务是分配N个人中的M对象,从位置S开始,使得第i个人得到至多ARR [i]每次都反对。
例子:
Input: arr[] = {2, 3, 2, 1, 4}, M = 11, S = 2
Output: 1, 3, 2, 1, 4
Explanation: The distribution of M (= 11) objects starting from Sth(= 2) person is as follows:
- For arr[2](= 3): Give 3 objects to the 2nd person. Now, the total number of objects reduces to (11 – 3) = 8.
- For arr[3] (= 2): Give 2 objects to the 3rd person. Now, the total number of objects reduces to (8 – 2) = 6.
- For arr[4] (= 1): Give 1 object to the 4th person. Now, the total number of objects reduces to (6 – 1) = 5.
- For arr[5] (= 4): Give 4 objects to the 5th person. Now, the total number of objects reduces to (5 – 4) = 1.
- For arr[1] (= 1): Give 1 object to the 1st person. Now, the total number of objects reduced to (1 – 1) = 0.
Therefore, the distribution of objects is {1, 3, 2, 1, 4}.
Input: arr[] = {2, 3, 2, 1, 4}, M = 3, S = 4
Output: 0 0 0 1 2
方法:可以通过从给定的起始索引S遍历数组并将最大对象分配给每个数组元素来解决给定的问题。请按照以下步骤解决给定的问题:
- 初始化一个辅助数组,即distribution [] ,将所有元素都设置为0,以存储M个对象的分布。
- 将两个变量ptr和rem分别初始化为S和M ,以存储起始索引和其余M个对象。
- 迭代直到rem为正,然后执行以下步骤:
- 如果rem的值至少是索引ptr上的元素,即arr [ptr] ,则将distribution [ptr]的值增加arr [ptr]并将rem的值减小arr [ptr] 。
- 否则,将distribution [ptr]增加rem并更新等于0的rem 。
- 更新PTR等于(PTR + 1)%N进行迭代的给定阵列ARR []以循环的方式。
- 完成上述步骤后,将distribution []打印为对象的结果分布。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find distribution of
// M objects among all array elements
void distribute(int N, int K,
int M, int arr[])
{
// Stores the distribution
// of M objects
int distribution[N] = { 0 };
// Stores the indices
// of distribution
int ptr = K - 1;
// Stores the remaining objects
int rem = M;
// Iterate until rem is positive
while (rem > 0) {
// If the number of remaining
// objects exceeds required
// the number of objects
if (rem >= arr[ptr]) {
// Increase the number of objects
// for the index ptr by arr[ptr]
distribution[ptr] += arr[ptr];
// Decrease remaining
// objects by arr[ptr]
rem -= arr[ptr];
}
else {
// Increase the number of objects
// for the index ptr by rem
distribution[ptr] += rem;
// Decrease remaining
// objects to 0
rem = 0;
}
// Increase ptr by 1
ptr = (ptr + 1) % N;
}
// Print the final distribution
for (int i = 0; i < N; i++) {
cout << distribution[i]
<< " ";
}
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 2, 1, 4 };
int M = 11, S = 2;
int N = sizeof(arr) / sizeof(arr[0]);
distribute(N, S, M, arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find distribution of
// M objects among all array elements
static void distribute(int N, int K, int M, int arr[])
{
// Stores the distribution
// of M objects
int distribution[] = new int[N];
// Stores the indices
// of distribution
int ptr = K - 1;
// Stores the remaining objects
int rem = M;
// Iterate until rem is positive
while (rem > 0) {
// If the number of remaining
// objects exceeds required
// the number of objects
if (rem >= arr[ptr]) {
// Increase the number of objects
// for the index ptr by arr[ptr]
distribution[ptr] += arr[ptr];
// Decrease remaining
// objects by arr[ptr]
rem -= arr[ptr];
}
else {
// Increase the number of objects
// for the index ptr by rem
distribution[ptr] += rem;
// Decrease remaining
// objects to 0
rem = 0;
}
// Increase ptr by 1
ptr = (ptr + 1) % N;
}
// Print the final distribution
for (int i = 0; i < N; i++) {
System.out.print(distribution[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 3, 2, 1, 4 };
int M = 11, S = 2;
int N = arr.length;
distribute(N, S, M, arr);
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to find distribution of
# M objects among all array elements
def distribute(N, K, M, arr):
# Stores the distribution
# of M objects
distribution = [0] * N
# Stores the indices
# of distribution
ptr = K - 1
# Stores the remaining objects
rem = M
# Iterate until rem is positive
while (rem > 0):
# If the number of remaining
# objects exceeds required
# the number of objects
if (rem >= arr[ptr]):
# Increase the number of objects
# for the index ptr by arr[ptr]
distribution[ptr] += arr[ptr]
# Decrease remaining
# objects by arr[ptr]
rem -= arr[ptr]
else:
# Increase the number of objects
# for the index ptr by rem
distribution[ptr] += rem
# Decrease remaining
# objects to 0
rem = 0
# Increase ptr by 1
ptr = (ptr + 1) % N
# Print the final distribution
for i in range(N):
print(distribution[i], end = " ")
# Driver Code
arr = [ 2, 3, 2, 1, 4 ]
M = 11
S = 2
N = len(arr)
distribute(N, S, M, arr)
# This code is contributed by sanjoy_62
C#
// C# program for the above approach
using System;
class GFG{
// Function to find distribution of
// M objects among all array elements
static void distribute(int N, int K,
int M, int []arr)
{
// Stores the distribution
// of M objects
int []distribution = new int[N];
// Stores the indices
// of distribution
int ptr = K - 1;
// Stores the remaining objects
int rem = M;
// Iterate until rem is positive
while (rem > 0)
{
// If the number of remaining
// objects exceeds required
// the number of objects
if (rem >= arr[ptr])
{
// Increase the number of objects
// for the index ptr by arr[ptr]
distribution[ptr] += arr[ptr];
// Decrease remaining
// objects by arr[ptr]
rem -= arr[ptr];
}
else
{
// Increase the number of objects
// for the index ptr by rem
distribution[ptr] += rem;
// Decrease remaining
// objects to 0
rem = 0;
}
// Increase ptr by 1
ptr = (ptr + 1) % N;
}
// Print the final distribution
for(int i = 0; i < N; i++)
{
Console.Write(distribution[i] + " ");
}
}
// Driver Code
public static void Main(string[] args)
{
int []arr = { 2, 3, 2, 1, 4 };
int M = 11, S = 2;
int N = arr.Length;
distribute(N, S, M, arr);
}
}
// This code is contributed by AnkThon
输出:
1 3 2 1 4
时间复杂度: O(M)
辅助空间: O(N)