给定一个正整数n,找到{1,2,.. n}在字典上最小的排列p,这样p i != iie,我不应该在i从1到n变化的第i个位置。
例子:
Input : 5
Output : 2 1 4 5 3
Consider the two permutations that follow
the requirement that position and numbers
should not be same.
p = (2, 1, 4, 5, 3) and q = (2, 4, 1, 5, 3).
Since p is lexicographically smaller, our
output is p.
Input : 6
Output : 2 1 4 3 6 5
由于我们需要字典上最小的(并且1不能位于位置1),因此我们将2放在第一位置。在2之后,我们放置下一个最小元素,即1。之后,考虑它的下一个最小元素不违反pi!= i的条件。
现在,如果我们的n是偶数,我们简单地取两个变量,一个将包含我们的偶数计数,另一个将包含我们的奇数计数,然后我们将它们加到向量中直到达到n。
但是,如果我们的n为奇数,我们将执行相同的任务,直到达到n-1,因为如果我们将其加至n,那么最后我们将最终得到p i = i。因此,当我们到达n-1时,我们首先将n添加到位置n-1,然后在位置n上放置n-2。
上述程序的实现如下。
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to print the permutation
void findPermutation(vector a, int n)
{
vector res;
// Initial numbers to be pushed to result
int en = 2, on = 1;
// If n is even
if (n % 2 == 0) {
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
res.push_back(en);
en += 2;
} else {
res.push_back(on);
on += 2;
}
}
}
// If n is odd
else {
for (int i = 0; i < n - 2; i++) {
if (i % 2 == 0) {
res.push_back(en);
en += 2;
} else {
res.push_back(on);
on += 2;
}
}
res.push_back(n);
res.push_back(n - 2);
}
// Print result
for (int i = 0; i < n; i++)
cout << res[i] << " ";
cout << "\n";
}
// Driver Code
int main()
{
long long int n = 9;
findPermutation(n);
return 0;
}
Java
// Java implementation of the above approach
import java.util.Vector;
class GFG {
// Function to print the permutation
static void findPermutation(int n) {
Vector res = new Vector();
// Initial numbers to be pushed to result
int en = 2, on = 1;
// If n is even
if (n % 2 == 0) {
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
res.add(en);
en += 2;
} else {
res.add(on);
on += 2;
}
}
} // If n is odd
else {
for (int i = 0; i < n - 2; i++) {
if (i % 2 == 0) {
res.add(en);
en += 2;
} else {
res.add(on);
on += 2;
}
}
res.add(n);
res.add(n - 2);
}
// Print result
for (int i = 0; i < n; i++) {
System.out.print(res.get(i) + " ");
}
System.out.println("");
}
// Driver Code
public static void main(String[] args) {
int n = 9;
findPermutation(n);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the above approach
# Function to print the permutation
def findPermutation(n) :
res = []
# Initial numbers to be pushed to result
en, on = 2, 1
# If n is even
if (n % 2 == 0) :
for i in range(n) :
if (i % 2 == 0) :
res.append(en)
en += 2
else :
res.append(on)
on += 2
# If n is odd
else :
for i in range(n-2) :
if (i % 2 == 0) :
res.append(en)
en += 2
else :
res.append(on)
on += 2
res.append(n)
res.append(n - 2)
# Print result
for i in range(n) :
print(res[i] ,end = " ")
print()
# Driver Code
if __name__ == "__main__" :
n = 9;
findPermutation(n)
# This code is contributed by Ryuga
C#
// C# implementation of the above approach
using System;
using System.Collections;
public class GFG {
// Function to print the permutation
static void findPermutation(int n) {
ArrayList res = new ArrayList();
// Initial numbers to be pushed to result
int en = 2, on = 1;
// If n is even
if (n % 2 == 0) {
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
res.Add(en);
en += 2;
} else {
res.Add(on);
on += 2;
}
}
} // If n is odd
else {
for (int i = 0; i < n - 2; i++) {
if (i % 2 == 0) {
res.Add(en);
en += 2;
} else {
res.Add(on);
on += 2;
}
}
res.Add(n);
res.Add(n - 2);
}
// Print result
for (int i = 0; i < n; i++) {
Console.Write(res[i] + " ");
}
Console.WriteLine("");
}
// Driver Code
public static void Main() {
int n = 9;
findPermutation(n);
}
}
// This code is contributed by 29AjayKumar
PHP
Javascript
输出:
2 1 4 3 6 5 8 9 7
时间复杂度: O(n)