重新排列给定的数组,使所有设置的位位置都比其他位置具有更高的值
给定一个数组B1[]和一个二进制数组B2[] ,每个数组的大小为N ,任务是重新排列数组B1[] ,使得对于B2[]的所有 setbit 位置i , B1[i]的值将是大于B2[]中未设置位的值,即对于所有(i, j) B1[i] > B1[j]当B2[i] = 1, B2[j] = 0 (0 ≤ i, j < N)。
注意:如果可以进行多种排列,请打印其中任何一种。
例子:
Input: N = 7, B1[] = {1, 2, 3, 4, 5, 6, 7}, B2[] = {0, 1, 0, 1, 0, 1, 0}
Output: 1 5 2 6 3 7 4
Explanation: Values having 1 in B2[] array are = {2, 4, 6} and values with 0s are = {1, 3, 5, 7}.
So, in correspondent to B2[] array, B1[] can be rearranged = {1, 5, 2, 6, 3, 7, 4}
B1[] = {1, 5, 2, 6, 3, 7, 4}
B2[] = {0, 1, 0, 1, 0, 1, 0}, now all places in B1[i] > B1[j] where B2[i] = 1, and B2[j] = 0.
It also can be rearranged as {1, 7, 2, 6, 3, 5, 4}
Input: N = 3, B1[] = {4, 3, 5}, B2[] = {1, 1, 1}
Output: 5 4 3
方法:可以根据以下思路解决问题:
To arrange B1[] as per set bit positions of B2[], sort the values of B1[] and arrange the higher values in positions with bits set and the lower values in positions in other positions.
按照下面提到的步骤来实现上述想法:
- 用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)