📌  相关文章
📜  词典上最大的N长度Bitonic序列,由给定范围内的元素组成

📅  最后修改于: 2021-05-06 09:59:20             🧑  作者: Mango

给定三个整数N (高) ,任务是找到字典上最大的双音序列,该序列由位于[low,high]范围内的N个元素组成。如果不可能生成这样的序列,则打印“ Not不可能”

例子:

的方法:我们的想法是找到的合适的指数所得到的序列中,然后保持1序列中的相邻元件之间的差,使得所形成的双调序列是字典顺序最大可能的。请按照以下步骤解决问题:

  • 初始化大小为N的数组A []以存储结果序列。
  • 初始化变量high_index = -1以将high的索引存储在A []中,并设置high_index = N –(high – low + 1)
  • 如果high_index > (N – 1)/ 2 ,则剩余的N / 2个元素不能以严格的递增顺序放置。因此,打印“不可能”。
  • 否则,请执行以下步骤:
    • 如果high_index≤0 ,则将high_index设置为1 ,因为开始时必须严格增加序列。
    • 保持与从范围[high_index,0],从一个值1的差的严格递减的序列。
    • 保持严格递减的序列与来自范围为1的差[high_index + 1,N – 1]开始的值(高- 1)。
  • 完成上述步骤后,打印数组A []中的所有元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the lexicographically
// largest bitonic sequence of size N
// elements lies in the range[low, high]
void LargestArray(int N, int low, int high)
{
    // Store index of highest element
    int high_index = N - (high - low + 1);
 
    // If high_index > (N-1)/2, then
    // remaining N/2 elements cannot
    // be placed in bitonic order
    if (high_index > (N - 1) / 2) {
        cout << "Not Possible";
        return;
    }
 
    // If high_index <= 0, then
    // set high_index as 1
    if (high_index <= 0)
        high_index = 1;
 
    // Stores the resultant sequence
    int A[N];
 
    // Store the high value
    int temp = high;
 
    // Maintain strictly decreasing
    // sequence from index high_index
    // to 0 starting with temp
    for (int i = high_index; i >= 0; i--) {
 
        // Store the value and decrement
        // the temp variable by 1
        A[i] = temp--;
    }
 
    // Maintain the strictly decreasing
    // sequence from index high_index + 1
    // to N - 1 starting with high - 1
    high -= 1;
 
    for (int i = high_index + 1; i < N; i++)
 
        // Store the value and decrement
        // high by 1
        A[i] = high--;
 
    // Print the resultant sequence
    for (int i = 0; i < N; i++) {
        cout << A[i] << ' ';
    }
}
 
// Driver Code
int main()
{
    int N = 5, low = 2, high = 6;
 
    // Function Call
    LargestArray(N, low, high);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
   
class GFG{
     
// Function to find the lexicographically
// largest bitonic sequence of size N
// elements lies in the range[low, high]
static void LargestArray(int N, int low,
                         int high)
{
     
    // Store index of highest element
    int high_index = N - (high - low + 1);
     
    // If high_index > (N-1)/2, then
    // remaining N/2 elements cannot
    // be placed in bitonic order
    if (high_index > (N - 1) / 2)
    {
        System.out.print("Not Possible");
        return;
    }
     
    // If high_index <= 0, then
    // set high_index as 1
    if (high_index <= 0)
        high_index = 1;
         
    // Stores the resultant sequence
    int[] A = new int[N];
  
    // Store the high value
    int temp = high;
  
    // Maintain strictly decreasing
    // sequence from index high_index
    // to 0 starting with temp
    for(int i = high_index; i >= 0; i--)
    {
         
        // Store the value and decrement
        // the temp variable by 1
        A[i] = temp--;
    }
  
    // Maintain the strictly decreasing
    // sequence from index high_index + 1
    // to N - 1 starting with high - 1
    high -= 1;
  
    for(int i = high_index + 1; i < N; i++)
     
        // Store the value and decrement
        // high by 1
        A[i] = high--;
  
    // Print the resultant sequence
    for(int i = 0; i < N; i++)
    {
        System.out.print(A[i] + " ");
    }
}
   
// Driver Code
public static void main(String[] args)
{
    int N = 5, low = 2, high = 6;
     
    // Function Call
    LargestArray(N, low, high);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
  
# Function to find the lexicographically
# largest bitonic sequence of size N
# elements lies in the range[low, high]
def LargestArray(N, low, high):
     
    # Store index of highest element
    high_index = N - (high - low + 1)
     
    # If high_index > (N-1)/2, then
    # remaining N/2 elements cannot
    # be placed in bitonic order
    if (high_index > (N - 1) // 2):
        print("Not Possible")
        return
     
    # If high_index <= 0, then
    # set high_index as 1
    if (high_index <= 0):
        high_index = 1
  
    # Stores the resultant sequence
    A = [0] * N
  
    # Store the high value
    temp = high
  
    # Maintain strictly decreasing
    # sequence from index high_index
    # to 0 starting with temp
    for i in range(high_index, -1, -1):
  
        # Store the value and decrement
        # the temp variable by 1
        A[i] = temp
        temp = temp - 1
     
    # Maintain the strictly decreasing
    # sequence from index high_index + 1
    # to N - 1 starting with high - 1
    high -= 1
  
    for i in range(high_index + 1, N):
         
        # Store the value and decrement
        # high by 1
        A[i] = high
        high = high - 1
  
    # Print the resultant sequence
    for i in range(N):
        print(A[i], end = " ")
 
# Driver Code
N = 5
low = 2
high = 6
  
# Function Call
LargestArray(N, low, high)
 
# This code is contributed by code_hunt


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the lexicographically
// largest bitonic sequence of size N
// elements lies in the range[low, high]
static void LargestArray(int N, int low,
                        int high)
{
     
    // Store index of highest element
    int high_index = N - (high - low + 1);
     
    // If high_index > (N-1)/2, then
    // remaining N/2 elements cannot
    // be placed in bitonic order
    if (high_index > (N - 1) / 2)
    {
        Console.Write("Not Possible");
        return;
    }
     
    // If high_index <= 0, then
    // set high_index as 1
    if (high_index <= 0)
        high_index = 1;
         
    // Stores the resultant sequence
    int[] A = new int[N];
 
    // Store the high value
    int temp = high;
 
    // Maintain strictly decreasing
    // sequence from index high_index
    // to 0 starting with temp
    for(int i = high_index; i >= 0; i--)
    {
         
        // Store the value and decrement
        // the temp variable by 1
        A[i] = temp--;
    }
 
    // Maintain the strictly decreasing
    // sequence from index high_index + 1
    // to N - 1 starting with high - 1
    high -= 1;
 
    for(int i = high_index + 1; i < N; i++)
     
        // Store the value and decrement
        // high by 1
        A[i] = high--;
 
    // Print the resultant sequence
    for(int i = 0; i < N; i++)
    {
        Console.Write(A[i] + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 5, low = 2, high = 6;
     
    // Function Call
    LargestArray(N, low, high);
}
}
 
// This code is contributed by shivanisinghss2110


输出:
5 6 5 4 3

时间复杂度: O(N)
辅助空间: O(1)