📌  相关文章
📜  执行给定操作后放置在每个盒子中的数据包计数

📅  最后修改于: 2021-05-19 19:28:32             🧑  作者: Mango

给定两个数组arr []operation [],它们NM个整数以及一个整数C组成。数组arr []代表N个盒子中的初始数据包数量,每个盒子的容量为C。从位置1开始,按数组operation []指定的顺序执行以下类型的操作:

  • 类型1:移到左侧框。
  • 类型2:移到右边的框。
  • 类型3:从当前框中选择一个数据包。
  • 类型4:从当前框中丢弃数据包。
  • 类型5:在每个框中打印数据包数量并退出。

在以下情况下,任何操作都将被忽略:

  • 如果当前位置为1,则将忽略向左移动操作。
  • 如果当前位置为N,则将忽略向右移动操作。
  • 如果已拾取数据包但尚未丢弃数据包或当前框为空,则“选择数据包”操作将被忽略。
  • 如果未选择任何数据包或当前盒中已经有C个数据包,则丢弃数据包操作将被忽略

例子:

方法:想法是为每个任务分配变量,以便对于当前索引,使用0初始化curr 。要检查元素是否被拾取,请使用false初始化被拾取的元素。请按照以下步骤解决问题:

  1. 保持一个布尔变量被选择,以跟踪是否已经选择了某些数据包。
  2. 如果操作类型为1并且curr不为0 ,则向左移动,将curr1
  3. 如果操作类型为2curr不是(N – 1) ,请向右移动,将current curr增加1
  4. 如果操作类型为3并且pickedfalse并且当前框不为空,则在当前框中将数据包减少1并标记pickedtrue
  5. 如果操作类型为4采摘真实,当前框不充分,在当前框增加包1和标记挑选
  6. 如果操作类型为5 ,则将当前值打印在数组arr []中并退出。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
#define ll long long int
 
// Function to print final array after
// performing all the operations
int printFinalArray(int* a, int n,
                    int* operations,
                    int p, int capacity)
{
 
    // Initialize variables
    int i, curr = 0;
    bool picked = false;
 
    // Traverse through all operations
    for (i = 0; i < p; i++) {
 
        // Operation Type
        int s = operations[i];
        bool flag = false;
 
        switch (s) {
 
        // Move left
        case 1:
            if (curr != 0)
                curr--;
            break;
 
        // Move right
        case 2:
            if (curr != n - 1)
                curr++;
            break;
 
        // Pick a packet
        case 3:
            if (picked == false
                && a[curr] != 0) {
                picked = true;
                a[curr]--;
            }
            break;
 
        // Drop a packet
        case 4:
            if (picked == true
                && a[curr] != capacity) {
                picked = false;
                a[curr]++;
            }
            break;
 
        // Exit
        default:
            flag = true;
        }
 
        if (flag == true)
            break;
    }
 
    // Print final array
    for (i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
}
 
// Driver Code
int main()
{
    // Given capacity
    int capacity = 5;
 
    // Given array with initial values
    int a[] = { 2, 5, 2 };
 
    // Array size
    int N = sizeof(a) / sizeof(a[0]);
 
    // Operations
    int operations[] = { 3, 2, 4, 1, 4, 5 };
 
    // Number of operations
    int M = sizeof(operations)
            / sizeof(operations[0]);
 
    // Function call
    printFinalArray(a, N, operations,
                    M, capacity);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to print final array after
// performing aint the operations
static void printFinalArray(int []a, int n,
                            int []operations,
                            int p, int capacity)
{
     
    // Initialize variables
    int i, curr = 0;
    boolean picked = false;
     
    // Traverse through aint operations
    for(i = 0; i < p; i++)
    {
         
        // Operation Type
        int s = operations[i];
        boolean flag = false;
         
        switch(s)
        {
             
            // Move left
            case 1:
                if (curr != 0)
                    curr--;
                break;
     
            // Move right
            case 2:
                if (curr != n - 1)
                    curr++;
                break;
     
            // Pick a packet
            case 3:
                if (picked == false &&
                    a[curr] != 0)
                {
                    picked = true;
                    a[curr]--;
                }
                break;
                 
            // Drop a packet
            case 4:
                if (picked == true && 
                    a[curr] != capacity)
                {
                    picked = false;
                    a[curr]++;
                }
                break;
                 
            // Exit
            default:
                flag = true;
        }
         
        if (flag == true)
            break;
    }
     
    // Print final array
    for(i = 0; i < n; i++)
    {
        System.out.print(a[i] + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given capacity
    int capacity = 5;
 
    // Given array with initial values
    int a[] = { 2, 5, 2 };
 
    // Array size
    int N = a.length;
 
    // Operations
    int operations[] = { 3, 2, 4, 1, 4, 5 };
 
    // Number of operations
    int M = operations.length;
 
    // Function call
    printFinalArray(a, N, operations,
                    M, capacity);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
 
# Function to print final array after
# performing all the operations
def printFinalArray(a, n,
                    operations, p, capacity):
 
    # Initialize variables
    curr = 0
    picked = False
 
    # Traverse through all operations
    for i in range(p):
 
        # Operation Type
        s = operations[i]
        flag = False
 
        # Move left
        if (curr != 0):
            curr -= 1
            break
 
        # Move right
        if (curr != n - 1):
            curr += 1
            break
 
        # Pick a packet
        if (picked == False
                and a[curr] != 0):
            picked = True
            a[curr] -= 1
            break
 
        # Drop a packet
        if (picked == True
                and a[curr] != capacity):
            picked = False
            a[curr] += 1
            break
 
        # Exit
        else:
            flag = True
 
        if (flag == True):
            break
 
    # Print final array
    for i in range(n):
        print(a[i], end=" ")
 
# Driver Code
if __name__ == "__main__":
 
    # Given capacity
    capacity = 5
 
    # Given array with initial values
    a = [2, 5, 2]
 
    # Array size
    N = len(a)
 
    # Operations
    operations = [3, 2, 4, 1, 4, 5]
 
    # Number of operations
    M = len(operations)
 
    # Function call
    printFinalArray(a, N, operations,
                    M, capacity)
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to print final array after
// performing aint the operations
static void printFinalArray(int []a, int n,
                            int []operations,
                            int p, int capacity)
{
     
    // Initialize variables
    int i, curr = 0;
    bool picked = false;
      
    // Traverse through aint operations
    for(i = 0; i < p; i++)
    {
          
        // Operation Type
        int s = operations[i];
        bool flag = false;
          
        switch(s)
        {
              
            // Move left
            case 1:
                if (curr != 0)
                    curr--;
                break;
      
            // Move right
            case 2:
                if (curr != n - 1)
                    curr++;
                break;
      
            // Pick a packet
            case 3:
                if (picked == false &&
                    a[curr] != 0)
                {
                    picked = true;
                    a[curr]--;
                }
                break;
                  
            // Drop a packet
            case 4:
                if (picked == true && 
                    a[curr] != capacity)
                {
                    picked = false;
                    a[curr]++;
                }
                break;
                  
            // Exit
            default:
                flag = true;
            break;
        }
          
        if (flag == true)
            break;
    }
      
    // Print final array
    for(i = 0; i < n; i++)
    {
        Console.Write(a[i] + " ");
    }
}
  
// Driver Code
public static void Main()
{
     
    // Given capacity
    int capacity = 5;
  
    // Given array with initial values
    int[] a = { 2, 5, 2 };
  
    // Array size
    int N = a.Length;
  
    // Operations
    int[] operations = { 3, 2, 4, 1, 4, 5 };
  
    // Number of operations
    int M = operations.Length;
  
    // Function call
    printFinalArray(a, N, operations,
                    M, capacity);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
2 5 2

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