给定两个数组P []和Q [] ,它们的排列顺序是前N个自然数。如果P []和Q []是第a个和b的第字典顺序排列最小[1,N]分别任务是找到| a − b | 。
例子 :
Input: P[] = {1, 3, 2}, Q[] = {3, 1, 2}
Output: 3
Explanation: 6 permutations of [1, 3] arranged lexicographically are {{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}. Therefore, rank of P[] is 2 and rank of Q[] is 5. Therefore, difference is |2 – 5| = 3.
Input: P[] = {1, 2}, Q[] = {1, 2}
Output: 0
Explanation: 2 permutations of [1, 2] arranged lexicographically are {{1, 2}, {2, 1}}. Therefore, rank of P[] is 1 and rank of Q[] is 1. Therefore, difference is |2-2| = 0.
方法:按照以下步骤解决问题:
- 使用next_permutation()函数查找两个排列的等级。
- 初始化数组temp []以存储前N个自然数的最小排列。另外,用0初始化两个变量a和b ,以存储两个排列的字典顺序。
- 检查temp []是否等于P [] 。如果发现是真实的,则跳出循环。否则,将a递增1并检查下一个排列
- 类似地,找到置换Q []的字典顺序,并将其存储在b中。
- 最后,打印出a和b之间的绝对差作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function the print the difference
// between the lexicographical ranks
void findDifference(vector& p,
vector& q, int N)
{
// Store the permutations
// in lexicographic order
vector A(N);
// Intital permutation
for (int i = 0; i < N; i++)
A[i] = i + 1;
// Initial variables
bool IsCorrect;
int a = 1, b = 1;
// Check permutation
do {
IsCorrect = true;
for (int i = 0; i < N; i++) {
if (A[i] != p[i]) {
IsCorrect = false;
break;
}
}
if (IsCorrect)
break;
a++;
} while (next_permutation(A.begin(),
A.end()));
// Intialize second permutation
for (int i = 0; i < N; i++)
A[i] = i + 1;
// Check permutation
do {
IsCorrect = true;
for (int i = 0; i < N; i++) {
if (A[i] != q[i]) {
IsCorrect = false;
break;
}
}
if (IsCorrect)
break;
b++;
} while (next_permutation(A.begin(),
A.end()));
// Print difference
cout << abs(a - b) << endl;
}
// Driver Code
int main()
{
// Given array P[]
vector p = { 1, 3, 2 };
// Given array Q[]
vector q = { 3, 1, 2 };
// Given size
int n = p.size();
// Function call
findDifference(p, q, n);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function the print the difference
// between the lexicographical ranks
static void findDifference(int[] p,
int[] q,
int N)
{
// Store the permutations
// in lexicographic order
int[] A = new int[N];
// Intital permutation
for(int i = 0; i < N; i++)
A[i] = i + 1;
// Initial variables
boolean IsCorrect;
int a = 1, b = 1;
// Check permutation
do
{
IsCorrect = true;
for(int i = 0; i < N; i++)
{
if (A[i] != p[i])
{
IsCorrect = false;
break;
}
}
if (IsCorrect)
break;
a++;
} while (next_permutation(A));
// Intialize second permutation
for(int i = 0; i < N; i++)
A[i] = i + 1;
// Check permutation
do
{
IsCorrect = true;
for(int i = 0; i < N; i++)
{
if (A[i] != q[i])
{
IsCorrect = false;
break;
}
}
if (IsCorrect)
break;
b++;
} while (next_permutation(A));
// Print difference
System.out.print(Math.abs(a - b) + "\n");
}
static boolean next_permutation(int[] p)
{
for(int a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for(int b = p.length - 1;; --b)
if (p[b] > p[a])
{
int t = p[a];
p[a] = p[b];
p[b] = t;
for(++a, b = p.length - 1;
a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
return false;
}
// Driver Code
public static void main(String[] args)
{
// Given array P[]
int[] p = { 1, 3, 2 };
// Given array Q[]
int[] q = { 3, 1, 2 };
// Given size
int n = p.length;
// Function call
findDifference(p, q, n);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function the print the difference
# between the lexicographical ranks
def findDifference(p, q, N):
# Store the permutations
# in lexicographic order
A = [0] * N
# Intital permutation
for i in range(N):
A[i] = i + 1
# Initial variables
IsCorrect = False
a, b = 1, 1
# Check permutation
while True:
IsCorrect = True
for i in range(N):
if (A[i] != p[i]):
IsCorrect = False
break
if (IsCorrect):
break
a += 1
if (not next_permutation(A)):
break
# Intialize second permutation
for i in range(N):
A[i] = i + 1
# Check permutation
while True:
IsCorrect = True
for i in range(N):
if (A[i] != q[i]):
IsCorrect = False
break
if (IsCorrect):
break
b += 1
if (not next_permutation(A)):
break
# Print difference
print(abs(a - b))
def next_permutation(p):
for a in range(len(p) - 2, -1, -1):
if (p[a] < p[a + 1]):
b = len(p) - 1
while True:
if (p[b] > p[a]):
t = p[a]
p[a] = p[b]
p[b] = t
a += 1
b = len(p) - 1
while a < b:
t = p[a]
p[a] = p[b]
p[b] = t
a += 1
b -= 1
return True
b -= 1
return False
# Driver Code
# Given array P[]
p = [ 1, 3, 2 ]
# Given array Q[]
q = [ 3, 1, 2 ]
# Given size
n = len(p)
# Function call
findDifference(p, q, n)
# This code is contributed by divyeshrabadiya07
C#
// C# program for the above approach
using System;
class GFG{
// Function the print the difference
// between the lexicographical ranks
static void findDifference(int[] p,
int[] q,
int N)
{
// Store the permutations
// in lexicographic order
int[] A = new int[N];
// Intital permutation
for(int i = 0; i < N; i++)
A[i] = i + 1;
// Initial variables
bool IsCorrect;
int a = 1, b = 1;
// Check permutation
do
{
IsCorrect = true;
for(int i = 0; i < N; i++)
{
if (A[i] != p[i])
{
IsCorrect = false;
break;
}
}
if (IsCorrect)
break;
a++;
} while (next_permutation(A));
// Intialize second permutation
for(int i = 0; i < N; i++)
A[i] = i + 1;
// Check permutation
do
{
IsCorrect = true;
for(int i = 0; i < N; i++)
{
if (A[i] != q[i])
{
IsCorrect = false;
break;
}
}
if (IsCorrect)
break;
b++;
} while (next_permutation(A));
// Print difference
Console.Write(Math.Abs(a - b) + "\n");
}
static bool next_permutation(int[] p)
{
for(int a = p.Length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for(int b = p.Length - 1;; --b)
if (p[b] > p[a])
{
int t = p[a];
p[a] = p[b];
p[b] = t;
for(++a, b = p.Length - 1;
a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
return false;
}
// Driver Code
public static void Main(String[] args)
{
// Given array P[]
int[] p = { 1, 3, 2 };
// Given array Q[]
int[] q = { 3, 1, 2 };
// Given size
int n = p.Length;
// Function call
findDifference(p, q, n);
}
}
// This code is contributed by Amit Katiyar
3
时间复杂度: O(N * max(a,b))其中N是排列的给定大小,而a和b是排列的字典顺序。
辅助空间: O(N)