📌  相关文章
📜  从两端生成具有相同大小 LIS 的 N 长度排列

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

从两端生成具有相同大小 LIS 的 N 长度排列

给定一个整数N ,任务是生成范围[1, N]中元素的排列,使得LIS从开始的长度等于 LIS 从数组末尾的长度。如果不存在这样的数组,则打印 -1。

例子:

方法:使用下面的观察来解决这个问题:

请按照以下步骤解决问题:

  • 如果提到的大小为 2,则返回 -1。
  • 否则,根据上述观察添加元素。
  • 返回形成的列表并打印列表元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to create array having
// same LIS from both sides
vector equalLIS(int N)
{
   
    // If N = 2 we can't create array
    if (N == 2) {
        vector al;
        al.push_back(-1);
        return al;
    }
 
    // If N is odd it is possible
    // to make an array
    else if (N % 2 != 0) {
 
        // Hash Map contains index
        // with its value
        map lis;
 
        // indices 0, N/2, and N-1
        lis.insert({ 0, 1 });
        lis.insert({ N - 1, 2 });
        int mid = N / 2;
        lis.insert({ mid, N });
 
        if (N > 3) {
 
            // Adding numbers 3, 4, ...
            // and so on at index from
            // 1 to mid-1 respectively
            int val = 3;
            for (int i = 1; i < mid; i++) {
                lis.insert({ i, val });
                val++;
            }
 
            // Adding remaining integers
            // in decreasing order
            // from mid+1 to N-2
            val = N - 1;
            for (int i = mid + 1; i < N - 1; i++) {
                lis.insert({ i, val });
                val--;
            }
        }
 
        // Creating Array List
        // which will use to form
        // required array
        vector al;
 
        // Traversing from smallest key
        // to largest key in Hash Map
        // and adding its value in list
        for (int i = 0; i < N; i++) {
            al.push_back(lis[i]);
        }
 
        // Returning formed array
        return al;
    }
    else {
 
        // Hash Map which contains
        // index with its value
        map lis;
 
        // Adding value for N=4 in Hash Map
        lis.insert({ 0, 2 });
        lis.insert({ 1, 1 });
        lis.insert({ 2, 4 });
        lis.insert({ 3, 3 });
 
        int i = 3;
        int idx = 0;
        if (N >= 6) {
            i++;
            idx--;
 
            // Adding new N at starting index
            // and N-1 at last index
            int val = 5;
            while (val <= N) {
                lis.insert({ i, val });
                lis.insert({ idx, val + 1 });
                idx--;
                i++;
                val += 2;
            }
        }
 
        // Creating Array List
        // which will use to form
        // required array
        vector al;
 
        // Traversing from minimum key
        // to maximum key add
        // adding its value in Array List
        for (int j = idx + 1; j < i; j++) {
            al.push_back(lis[j]);
        }
 
        // Returning formed array
        return al;
    }
}
 
// Driver Code
int main()
{
    int N = 7;
    vector lis = equalLIS(N);
    for (auto x : lis)
        cout << x << " ";
    return 0;
}
 
    // This code is contributed by rakeshsahni


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG {
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 7;
        List lis = equalLIS(N);
        for (int x : lis)
            System.out.print(x + " ");
    }
 
    // Function to create array having
    // same LIS from both sides
    public static List equalLIS(int N)
    {
        // If N = 2 we can't create array
        if (N == 2) {
            List al = new ArrayList<>();
            al.add(-1);
            return al;
        }
 
        // If N is odd it is possible
        // to make an array
        else if (N % 2 != 0) {
 
            // Hash Map contains index
            // with its value
            Map lis
                = new HashMap<>();
 
            // Adding 1, N, and 2 at
            // indices 0, N/2, and N-1
            lis.put(0, 1);
            lis.put(N - 1, 2);
            int mid = N / 2;
            lis.put(mid, N);
 
            if (N > 3) {
 
                // Adding numbers 3, 4, ...
                // and so on at index from
                // 1 to mid-1 respectively
                int val = 3;
                for (int i = 1; i < mid; i++) {
                    lis.put(i, val);
                    val++;
                }
 
                // Adding remaining integers
                // in decreasing order
                // from mid+1 to N-2
                val = N - 1;
                for (int i = mid + 1;
                     i < N - 1; i++) {
                    lis.put(i, val);
                    val--;
                }
            }
 
            // Creating Array List
            // which will use to form
            // required array
            List al
                = new ArrayList<>();
 
            // Traversing from smallest key
            // to largest key in Hash Map
            // and adding its value in list
            for (int i = 0; i < N; i++) {
                al.add(lis.get(i));
            }
 
            // Returning formed array
            return al;
        }
        else {
 
            // Hash Map which contains
            // index with its value
            Map lis
                = new HashMap<>();
 
            // Adding value for N=4 in Hash Map
            lis.put(0, 2);
            lis.put(1, 1);
            lis.put(2, 4);
            lis.put(3, 3);
 
            int i = 3;
            int idx = 0;
            if (N >= 6) {
                i++;
                idx--;
 
                // Adding new N  at starting index
                // and N-1 at last index
                int val = 5;
                while (val <= N) {
                    lis.put(i, val);
                    lis.put(idx, val + 1);
                    idx--;
                    i++;
                    val += 2;
                }
            }
 
            // Creating Array List
            // which will use to form
            // required array
            List al
                = new ArrayList<>();
 
            // Traversing from minimum key
            // to maximum key add
            // adding its value in Array List
            for (int j = idx + 1; j < i; j++) {
                al.add(lis.get(j));
            }
 
            // Returning formed array
            return al;
        }
    }
}


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Driver Code
    public static void Main(string[] args)
    {
        int N = 7;
        List lis = equalLIS(N);
        for (int i = 0; i < lis.Count; i++)
            Console.Write(lis[i] + " ");
    }
 
    // Function to create array having
    // same LIS from both sides
    public static List equalLIS(int N)
    {
        // If N = 2 we can't create array
        if (N == 2) {
            List al = new List();
            al.Add(-1);
            return al;
        }
 
        // If N is odd it is possible
        // to make an array
        else if (N % 2 != 0) {
 
            // Hash Map contains index
            // with its value
            Dictionary lis
                = new Dictionary();
 
            // Adding 1, N, and 2 at
            // indices 0, N/2, and N-1
            lis[0] = 1;
            lis[N - 1] = 2;
            int mid = N / 2;
            lis[mid] = N;
 
            if (N > 3) {
 
                // Adding numbers 3, 4, ...
                // and so on at index from
                // 1 to mid-1 respectively
                int val = 3;
                for (int i = 1; i < mid; i++) {
                    lis[i] = val;
                    val++;
                }
 
                // Adding remaining integers
                // in decreasing order
                // from mid+1 to N-2
                val = N - 1;
                for (int i = mid + 1; i < N - 1; i++) {
                    lis[i] = val;
                    val--;
                }
            }
 
            // Creating Array List
            // which will use to form
            // required array
            List al = new List();
 
            // Traversing from smallest key
            // to largest key in Hash Map
            // and adding its value in list
            for (int i = 0; i < N; i++) {
                al.Add(lis[i]);
            }
 
            // Returning formed array
            return al;
        }
        else {
 
            // Hash Map which contains
            // index with its value
            Dictionary lis
                = new Dictionary();
 
            // Adding value for N=4 in Hash Map
            lis[0] = 2;
            lis[1] = 1;
            lis[2] = 4;
            lis[3] = 3;
 
            int i = 3;
            int idx = 0;
            if (N >= 6) {
                i++;
                idx--;
 
                // Adding new N  at starting index
                // and N-1 at last index
                int val = 5;
                while (val <= N) {
                    lis[i] = val;
                    lis[idx] = val + 1;
                    idx -= 1;
                    i += 1;
                    val += 2;
                }
            }
 
            // Creating Array List
            // which will use to form
            // required array
            List al = new List();
 
            // Traversing from minimum key
            // to maximum key add
            // adding its value in Array List
            for (int j = idx + 1; j < i; j++) {
                al.Add(lis[j]);
            }
 
            // Returning formed array
            return al;
        }
    }
}
 
// This code is contributed by ukasp.



输出
1 3 4 7 6 5 2 

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