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

📅  最后修改于: 2021-05-13 23:43:03             🧑  作者: Mango

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

例子:

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

  • 初始化一个辅助数组,即distribution [] ,将所有元素都设置为0,以存储M个对象的分布。
  • 将两个变量ptrrem分别初始化为SM ,以存储起始索引和其余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)