📌  相关文章
📜  通过从 [1, N] 中选择 N/2 个元素,首先打印所有子序列,然后再增加

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

通过从 [1, N] 中选择 N/2 个元素,首先打印所有子序列,然后再增加

给定一个正整数N ,任务是打印所有子序列 通过从1N选择ceil(N/2)元素,子序列首先减少然后增加。

例子:

方法:该方法基于使用Python itertools.permutation 的内置函数来生成大小为 ceil(N/2) 的所有子序列。请按照以下步骤解决问题:

  • 初始化一个数组arr[]并插入从1N的所有元素。
  • 将变量K初始化为ceil(N/2)
  • 使用名为itertools.permutations(arr, k)的内置函数将所有子序列插入数组“ sequence ”中
  • 使用变量seq遍历数组序列并执行以下步骤:
    • 检查seq[1]>seq[0]seq[-1]< seq[-2]或者如果seq增加或减少则继续,否则该序列不满足上述条件。
    • 如果seq[i]seq[i] 有超过 1 个元素,那么也继续并且不打印数组。
    • 如果seq不遵循上述任何一点,则打印seq

下面是上述方法的实现:

C++
// cpp program for the above approach
#include
using namespace std;
 
// Function to check if the sequence is valid
// or not
bool  ValidSubsequence(vectorseq)
{
 
  // If first element is greater or last second
  // element is greater than last element
  int n = seq.size();
  if ((seq[0] < seq[1]) || (seq[n - 1] < seq[n - 2]))
    return false;
  int i = 0;
 
  // If the sequence is decreasing or increasing
  // or sequence is increasing return false
  // return 0;
  while (i<(seq.size() - 1))
  {
    if (seq[i] > seq[i + 1])
    {
      i++;
      continue;
    }
    else if (seq[i] < seq[i + 1]){
 
      int j = i + 1;
      if ((j != (seq.size())-1) && (seq[j]>seq[j + 1]))
        return false;
    }
    i+= 1;
 
  }
  // If the sequence do not follow above condition
  // Return True
  return true;
 
}
 
 
int main(){
  int N = 5;
  int K = (N+1)/2;
  vectorarr,arr0;
  for(int i = 0; i < N; i++)
  {
    arr.push_back(i+1);
  }
 
  // Generate all permutation of size N / 2 using
  // default function
  vector>sequences;
  do{
    vectortemp;
    for(int i = 0; i < K; i++)
    {
      temp.push_back(arr[i]);
    }
    sequences.push_back(temp);
  }while(next_permutation(arr.begin(),arr.end()));
 
  // Print the sequence which is valid valley subsequence
  map,int>vis;
  for (auto seq :sequences)
  {
 
    // Check whether the seq is valid or not
    // Function Call
    if (ValidSubsequence(seq) && !vis[seq])
    {
      vis[seq] = 1;
      for(auto j:seq){
        cout<


Python3
# Python3 program for the above approach
 
# import the ceil permutations in function
from math import ceil
 
# Function to generate all the permutations
from itertools import permutations
 
# Function to check if the sequence is valid
# or not
def ValidSubsequence(seq):
   
  # If first element is greater or last second
  # element is greater than last element
  if (seq[0]seq[i + 1]:
      pass
    elif seq[i]seq[j + 1]):
        return False
    i+= 1
     
   # If the sequence do not follow above condition
   # Return True
  return True
 
         
# Driver code
N = 5
K = ceil(N / 2)
arr = list(range(1, N + 1))
 
# Generate all permutation of size N / 2 using
# default function
sequences = list(permutations(arr, K))
 
# Print the sequence which is valid valley subsequence
for seq in sequences:
  # Check whether the seq is valid or not
  # Function Call
  if ValidSubsequence(seq):
    print(seq, end =" ")


输出:
(2, 1, 3) (2, 1, 4) (2, 1, 5) (3, 1, 2) (3, 1, 4) (3, 1, 5) (3, 2, 4) (3, 2, 5) (4, 1, 2) (4, 1, 3) (4, 1, 5) (4, 2, 3) (4, 2, 5) (4, 3, 5) (5, 1, 2) (5, 1, 3) (5, 1, 4) (5, 2, 3) (5, 2, 4) (5, 3, 4)

时间复杂度: O(C N N/2 * ceil(N/2)! *N)
辅助空间: O(C N N/2 * ceil(N/2)!)