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

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

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

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

    例子:

    方法:可以使用贪婪技术解决问题。这个想法是使用Priority Queue选择全0的最右边最长的子数组。请按照以下步骤解决问题:

    • 初始化一个优先级队列,例如pq ,以存储{X,Y}形式的子数组,其中X表示子数组的长度, Y表示子数组的起始索引。
    • 最初全为0的子数组的最大长度为N ,子数组的起始索引为0 。因此,将{N,0}插入pq
    • 使用变量i遍历[1,K]范围。对于每个i次操作,从pq弹出顶部元素,然后检查弹出元素的长度是否为奇数。如果确定为真,则将子数组的mid元素替换为i
    • 否则,如果是一个偶数,然后用替换子阵列的最右边的中期因素。否则,将子数组的最左边的mid元素替换为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)