📌  相关文章
📜  重新排列给定的数组,使所有设置的位位置都比其他位置具有更高的值

📅  最后修改于: 2022-05-13 01:56:06.247000             🧑  作者: Mango

重新排列给定的数组,使所有设置的位位置都比其他位置具有更高的值

给定一个数组B1[]和一个二进制数组B2[] ,每个数组的大小为N ,任务是重新排列数组B1[] ,使得对于B2[]的所有 setbit 位置iB1[i]的值将是大于B2[]中未设置位的值,即对于所有(i, j) B1[i] > B1[j]B2[i] = 1, B2[j] = 0 (0 ≤ i, j < N)。

注意:如果可以进行多种排列,请打印其中任何一种。

例子:

方法:可以根据以下思路解决问题:

按照下面提到的步骤来实现上述想法:

  • B2[] 的位值和 B1[]相应值制作一个对列表(比如v ),
  • B2[]的位值升序对对列表进行排序
  • 使用两个指针方法重新排列B1[]
    • 0声明指针,用N-1声明指针。
    • B2[i] 为0 (0 ≤ i < N) 时,将B1[]设置为v[left]并递增left
    • 否则,将B1[i]设置为v[right]并递减right
  • 返回B1[]作为最终答案。

以下是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to do required
// operation
void solve(int n, vector& b1, int b2[])
{
    //  Initializing vector of pair
    // to store b2 and b1 array element
    vector > v;
 
    int cnt = 1;
    for (int i = 0; i < n; i++) {
 
        // Pushing 1 and 0 integer along with
        // their corresponding element
        // in array b1
        v.push_back({ b2[i], b1[i] });
    }
 
    // Sorting the vector of pair
    // in ascending order
    sort(v.begin(), v.end());
 
    int left = 0, right = n - 1;
 
    // Arranging the array b1
    for (int i = 0; i < n; i++) {
        if (b2[i] == 0)
            b1[i] = v[left++].second;
        else
            b1[i] = v[right--].second;
    }
}
 
// Driver Code
int main()
{
    int N = 3;
    vector B1 = { 3, 4, 5 };
    int B2[] = { 1, 1, 1 };
 
    solve(N, B1, B2);
    for (int x : B1)
        cout << x << " ";
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
// User defined Pair class
class Pair {
  int x;
  int y;
 
  // Constructor
  public Pair(int x, int y)
  {
    this.x = x;
    this.y = y;
  }
}
 
// class to define user defined conparator
class Compare {
 
  static void compare(Pair arr[], int n)
  {
    // Comparator to sort the pair according to second element
    Arrays.sort(arr, new Comparator() {
      @Override public int compare(Pair p1, Pair p2)
      {
        return p1.x - p2.x; // To compare the first element just
        //change the variable from p1.y - p2.y to x.
      }
    });
  }
}
 
class GFG {
 
  // Function to do required
  // operation
  static void solve(int n, int[] b1, int[] b2)
  {
     
    //  Initializing vector of pair
    // to store b2 and b1 array element
    // Array of Pair
    Pair v[] = new Pair[n];
 
    int cnt = 1;
    for (int i = 0; i < n; i++) {
 
      // Pushing 1 and 0 integer along with
      // their corresponding element
      // in array b1
      v[i] = new Pair(b2[i], b1[i]);
    }
 
    // Sorting the vector of pair
    // in ascending order
    Compare obj = new Compare();
    obj.compare(v, n);
 
    int left = 0, right = n - 1;
 
    // Arranging the array b1
    for (int i = 0; i < n; i++) {
      if (b2[i] == 0)
        b1[i] = v[left++].y;
      else
        b1[i] = v[right--].y;
    }
  }
 
  // Driver Code
  public static void main (String[] args) {
    int N = 3;
    int B1[] = { 3, 4, 5 };
    int B2[] = { 1, 1, 1 };
 
    solve(N, B1, B2);
    for (int i = 0; i 


Python3
# Python3 program for the above approach
# Function to do required operation
def solve(n, b1, b2):
   
    # Initializing list of pair to store b2 and b1 array element
    v = []
    cnt = 1
    for i in range(n):
        # Pushing 1 and 0 integer along with
        # their corresponding element in array b1
        v.append([b2[i], b1[i]])
     
    v.sort()
    left = 0
    right = n - 1
     
    # Arranging the array b1
    for i in range(n):
        if b2[i] == 0:
            b1[i] = v[left][1]
            left += 1
        else:
            b1[i] = v[right][1]
            right -= 1
             
# Driver Code
N = 3
b1 = [3, 4, 5]
b2 = [1, 1, 1]
solve(N, b1, b2)
for x in b1:
    print(x, end = " ")
''' This code is written by Rajat Kumar (GLA University)'''


Javascript



输出
5 4 3 

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