用 A 0 和 X 反转计数构造大小为 N 的字典最小二进制数组
给定三个数字N 、 A和X ,任务是构建大小为N的字典最小二进制数组,其中包含A 0 s 并且反转计数为X 。
例子:
Input: N=5, A=2, X=1
Output: 0 1 0 1 1
Explanation:
The number of inversions in this array is 1(2nd and 3rd index).
Input: N=5, A=2, X=3
Output: 0 1 1 1 0
方法:给定的问题可以使用基于以下观察的两个指针技术来解决:
- The array with A 0s having 0 inversion is the array with all 0s to the beginning and then the all the 1s.
- If an element 0 at index i and an element 1 at index j is swapped, then inversion count increases by count of 1s in the range [i, j].
- The maximum possible inversion count is A*(N-A).
请按照以下步骤解决问题:
- 如果X大于A*(NA) ,打印-1然后返回。
- 初始化一个大小为N的数组arr[]并用0填充第一个A索引,其余的用1填充。
- 将两个变量curr初始化为A-1并将prev初始化为N-1以遍历数组。
- 迭代直到X大于0并且curr不小于0 ,并执行以下步骤:
- 如果X大于或等于prev-cur ,则执行以下操作:
- 在arr[prev]和arr[curr] 处交换两个元素。
- 从X中减去prev-cur 。
- 将prev和curr减1。
- 否则,请执行以下操作:
- 交换两个元素arr[curr]和arr[cur+1] 。
- 将curr增加1和 将X减1。
- 如果X大于或等于prev-cur ,则执行以下操作:
- 打印数组arr。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to construct lexicographically
// smallest binary string of length N, having
// A 0s and X inversions
void binaryArrayInversions(int N, int A, int X)
{
// If X inversions are not possible
if (A * (N - A) < X) {
cout << "-1";
return;
}
// Initialize array and fill with 0
int Arr[N] = { 0 };
// Fill last N-A indices with 1
fill(Arr + A, Arr + N, 1);
// Stores the index of current 0
int cur = A - 1;
// Stores the index of current 1
int prev = N - 1;
// Iterate until X is greater than
// 0 and cur is greater than equal
// to 0
while (X && cur >= 0) {
// If X is greater than or
// equal to the prev-cur
if (X >= prev - cur) {
// Swap current 0 and current 1
swap(Arr[prev], Arr[cur]);
// Update X
X -= prev - cur;
// Decrement prev and cur by 1
prev--;
cur--;
}
// Otherwise
else {
// Swap current 0 with the next index
swap(Arr[cur], Arr[cur + 1]);
// Increment cur by 1
cur++;
// Decrement X by 1
X--;
}
}
// Print the array
for (auto u : Arr)
cout << u << " ";
}
// Driver code
int main()
{
// Input
int N = 5;
int A = 2;
int X = 1;
// Function call
binaryArrayInversions(N, A, X);
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
class GFG{
// Function to construct lexicographically
// smallest binary string of length N, having
// A 0s and X inversions
static void binaryArrayInversions(int N, int A, int X)
{
// If X inversions are not possible
if (A * (N - A) < X)
{
System.out.println("-1");
return;
}
// Initialize array and fill with 0
int []Arr = new int[N];
// Fill last N-A indices with 1
Arrays.fill(Arr, 0);
for(int i = A; i < N; i++)
Arr[i] = 1;
// Stores the index of current 0
int cur = A - 1;
// Stores the index of current 1
int prev = N - 1;
// Iterate until X is greater than
// 0 and cur is greater than equal
// to 0
while (X != 0 && cur >= 0)
{
// If X is greater than or
// equal to the prev-cur
if (X >= prev - cur)
{
// Swap current 0 and current 1
int temp = Arr[prev];
Arr[prev] = Arr[cur];
Arr[cur] = temp;
// Update X
X -= prev - cur;
// Decrement prev and cur by 1
prev--;
cur--;
}
// Otherwise
else
{
// Swap current 0 with the next index
int temp = Arr[cur];
Arr[cur] = Arr[cur + 1];
Arr[cur + 1] = temp;
// Increment cur by 1
cur++;
// Decrement X by 1
X--;
}
}
// Print the array
for(int i = 0; i < Arr.length; i++)
System.out.print(Arr[i] + " ");
}
// Driver code
public static void main(String args[])
{
// Input
int N = 5;
int A = 2;
int X = 1;
// Function call
binaryArrayInversions(N, A, X);
}
}
// This code is contributed by gfgking
Python3
# Python3 program for the above approach
# Function to construct lexicographically
# smallest binary string of length N, having
# A 0s and X inversions
def binaryArrayInversions(N, A, X):
# If X inversions are not possible
if (A * (N - A) < X):
print("-1")
return
# Initialize array and fill with 0
Arr = [0]*N
for i in range(A,N):
Arr[i]=1
# Stores the index of current 0
cur = A - 1
# Stores the index of current 1
prev = N - 1
# Iterate until X is greater than
# 0 and cur is greater than equal
# to 0
while (X and cur >= 0):
# If X is greater than or
# equal to the prev-cur
if (X >= prev - cur):
# Swap current 0 and current 1
Arr[prev], Arr[cur] = Arr[cur],Arr[prev]
# Update X
X -= prev - cur
# Decrement prev and cur by 1
prev -= 1
cur -= 1
# Otherwise
else:
# Swap current 0 with the next index
Arr[cur], Arr[cur + 1] = Arr[cur + 1], Arr[cur]
# Increment cur by 1
cur += 1
# Decrement X by 1
X -= 1
# Print the array
for u in Arr:
print(u, end = " ")
# Driver code
if __name__ == '__main__':
# Input
N = 5
A = 2
X = 1
# Function call
binaryArrayInversions(N, A, X)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to construct lexicographically
// smallest binary string of length N, having
// A 0s and X inversions
static void binaryArrayInversions(int N, int A, int X)
{
// If X inversions are not possible
if (A * (N - A) < X) {
Console.Write("-1");
return;
}
// Initialize array and fill with 0
int []Arr = new int[N];
// Fill last N-A indices with 1
Array.Clear(Arr, 0, N);
for(int i=A;i= 0)
{
// If X is greater than or
// equal to the prev-cur
if (X >= prev - cur)
{
// Swap current 0 and current 1
int temp = Arr[prev];
Arr[prev] = Arr[cur];
Arr[cur] = temp;
// Update X
X -= prev - cur;
// Decrement prev and cur by 1
prev--;
cur--;
}
// Otherwise
else {
// Swap current 0 with the next index
int temp = Arr[cur];
Arr[cur] = Arr[cur + 1];
Arr[cur + 1] = temp;
// Increment cur by 1
cur++;
// Decrement X by 1
X--;
}
}
// Print the array
for(int i = 0; i < Arr.Length; i++)
Console.Write(Arr[i] +" ");
}
// Driver code
public static void Main()
{
// Input
int N = 5;
int A = 2;
int X = 1;
// Function call
binaryArrayInversions(N, A, X);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出
0 1 0 1 1
时间复杂度: O(N)
辅助空间: O(1)