📌  相关文章
📜  将最长的 0 子数组的中间元素从右边替换正好 K 次

📅  最后修改于: 2021-10-26 02:38:19             🧑  作者: Mango

给定一个大小为N的数组 arr[] ,最初由0 s 和一个正整数K 组成,任务是通过执行恰好K次以下操作来打印数组元素。

  • 对于每个i个操作,选择最右边的最长子数组,由所有 0组成,并用 i替换子数组的中间元素。
  • 如果存在两个中间元素,则检查i是否为偶数。如果发现为真,则用i替换最右边的中间元素。
  • 否则,用 i替换最左边的中间元素。

    例子:

    方法:该问题可以使用贪心技术解决。这个想法是使用优先级队列来选择最右边的最长的全为 0 的子数组。请按照以下步骤解决问题:

    • 初始化一个优先级队列,比如pq ,以存储{ X, Y}形式的子数组,其中X表示子数组的长度, Y表示子数组的起始索引。
    • 最初全为 0的子数组的最大长度为 N ,子数组的起始索引为0 。因此,将{ N, 0 }插入pq
    • 使用变量i在范围[1, K] 上迭代。对于每个i个操作,从pq弹出顶部元素并检查弹出元素的长度是否为奇数。如果发现为真,则用i替换子数组的中间元素。
    • 否则,如果是一个偶数,然后用替换子阵列的最右边的中期因素。否则,用 i替换子数组最左边的中间元素。
    • 用 i替换 mid 元素后,将子数组的左半部分和包含所有0子数组的右半部分插入 pq
    • 最后,打印数组元素。

    下面是上述方法的实现:

    C++
    // C++ program to implement
    // the above approach
      
      
    #include 
    using namespace std;
      
      
      
    // Function to print array by replacing the mid
    // of the righmost longest subarray with count
    // of operations performed on the array 
    void ReplaceArray(int arr[], int N, int K)
    {
          
          
        // Stores subarray of the form { X, Y },
        // where X is the length and Y is start
        // index of the subarray
        priority_queue > pq;
          
          
          
        // Insert the array arr[] 
        pq.push({ N, 0 });
          
          
          
        // Stores index of mid 
        // element of the subarray
        int mid;
          
          
          
        // Iterate over the range [1, N]
        for (int i = 1; i <= K; i++) {
              
              
              
            // Stores top element of pq
            vector sub = pq.top();
              
              
              
            // Pop top element of pq
            pq.pop();
              
              
              
            // If length of the subarray
            // is an odd number
            if (sub[0] % 2 == 1) {
                  
                  
                  
                // Update mid
                mid = sub[1] + sub[0] / 2;
                  
                  
                  
                // Replacing arr[mid] with i
                arr[mid] = i;
                  
                  
                  
                // Insert left half of
                // the subarray into pq
                pq.push({ sub[0] / 2,
                             sub[1] });
                  
                  
                  
                // Insert right half of
                // the subarray into pq
                pq.push({ sub[0] / 2, 
                               (mid + 1) });
            }
              
              
              
            // If length of the current 
            // subarray is an even number
            else {
                  
                  
                  
                // If i is 
                // an odd number
                if (i % 2 == 1) {
                      
                      
                      
                    // Update mid
                    mid = sub[1] + sub[0] / 2;
                      
                      
                      
                    // Replacing mid element
                    // with i
                    arr[mid - 1] = i;
                      
                      
                      
                    // Insert left half of
                    // the subarray into pq
                    pq.push({ sub[0] / 2 - 1, 
                                   sub[1] });
                      
                      
                      
                    // Insert right half of
                    // the subarray into pq
                    pq.push({ sub[0] / 2, mid });
                }
                  
                  
                  
                // If i is an even number
                else {
                      
                      
                      
                    // Update mid
                    mid = sub[1] + sub[0] / 2;
                      
                      
                      
                    // Replacing mid element
                    // with i
                    arr[mid - 1] = i;
                      
                      
                      
                    // Insert left half of
                    // the subarray into pq
                    pq.push({ sub[0] / 2,
                                sub[1] });
                      
                      
                      
                    // Insert right half of
                    // the subarray into pq
                    pq.push({ sub[0] / 2 - 1,
                               (mid + 1) });
                }
            }
        }
          
          
          
        // Print array elements
        for (int i = 0; i < N; i++)
            cout << arr[i] << " ";
    }
      
      
    // Driver Code
    int main()
    {
          
        int arr[] = { 0, 0, 0, 0, 0 };
        int N = sizeof(arr) / sizeof(arr[0]);
        int K = 3;
        ReplaceArray(arr, N, K);
    }


    输出:
    3 0 1 2 0
    

    时间复杂度: O(K * log(N))
    辅助空间: O(N)

    如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。