📜  从第 S 个人开始分配 M 个对象,使得每个第 i 个人都得到 arr[i] 个对象

📅  最后修改于: 2021-09-07 04:27:15             🧑  作者: Mango

给定的阵列ARR []N个整数(基于1的索引)和两个整数MS的,任务是分配N个人中的M对象,从位置S开始,使得i人得到至多ARR [i]对象每次。

例子:

方法:可以通过从给定的起始索引S遍历数组并将最大对象分配到每个数组元素来解决给定的问题。请按照以下步骤解决给定的问题:

  • 初始化一个辅助数组,比如distribution[] ,所有元素都为0来存储M 个对象的分布。
  • 初始化两个变量,分别说ptrremSM ,以存储起始索引和剩余的M 个对象
  • 迭代直到rem为正,执行以下步骤:
    • 如果rem的值至少是索引ptr的元素,即arr[ptr] ,则将distribution[ptr]的值增加arr[ptr]并将rem的值减少arr[ptr]
    • 否则,将distribution[ptr]增加rem并更新rem等于0
    • 更新ptr等于(ptr + 1) % N以循环方式迭代给定数组arr[]
  • 完成上述步骤后,打印分布[]作为对象的结果分布。

下面是上述方法的实现:

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


Javascript


输出:
1 3 2 1 4

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live