📌  相关文章
📜  为给定数组获得严格的LIS所需的最小连接

📅  最后修改于: 2021-05-05 01:40:45             🧑  作者: Mango

给定大小为n的数组A [] ,其中数组中只有唯一元素。我们必须找到序列A所需的最小串联,才能获得严格的最长递增子序列。对于数组A [],我们遵循基于1的索引。

例子:

方法:
为了解决上述问题,第一个观察结果是严格增加的子序列的长度始终等于序列A []中存在的唯一元素的数量。因此,我们必须找出一种方法来保持最大连续数字顺序在一起。我们可以通过在选择串联之前在序列中获取尽可能多的严格连续的数字并在下一个串联中处理其他数字来实现此目的。

  • 形成元素对及其索引,并按值按排序顺序存储它们。初始化变量以计算所需的串联操作。
  • 运行一个从索引2到n的循环,如果对pair [i-1]的索引> pair [i]的索引,则将变量递增1,该变量正在计算所需的串联操作,否则继续执行该过程。

下面是上述方法的实现:

C++
// CPP implementation to Find the minimum
// concatenation required to get strictly
// Longest Increasing Subsequence for the
// given array
#include 
using namespace std;
 
int increasingSubseq(int a[], int n)
{
    // Ordered map containing pairs
    // of value and index.
    map mp;
 
    for (int i = 0; i < n; i++) {
        // Form pairs of value at index i
        // and it's index that is i.
        mp.insert(make_pair(a[i], i));
    }
 
    // Variable to insert the minimum
    // concatenations that are needed
    int ans = 1;
 
    // Iterator pointing to the
    // second pair in the map
    auto itr = ++mp.begin();
 
    // Iterator pointing to the
    // first pair in the map
    for (auto it = mp.begin(); it != mp.end(); ++it) {
 
        // Check if itr tends to end of map
        if (itr != mp.end()) {
 
            // Check if index2 is not greater than index1
            // then we have to perform a concatenation.
            if (itr->second <= it->second)
                ans += 1;
 
            ++itr;
        }
    }
 
    // Return the final answer
    cout << ans << endl;
}
 
// Driver code
int main()
{
    int a[] = { 2, 1, 4, 3, 5 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    increasingSubseq(a, n);
 
    return 0;
}


Java
/*package whatever //do not write package name here */
import java.util.*;
import java.io.*;
 
class GFG {
 
    private static void increasingSubseq(int a[], int n)
    {
 
        // Ordered map containing pairs
        // of value and index.
        TreeMap mp
            = new TreeMap();
 
        for (int i = 0; i < n; i++) {
            // Form pairs of value at index i
            // and it's index that is i.
            mp.put(a[i], i);
        }
 
        // Variable to insert the minimum
        // concatenations that are needed
        int ans = 1;
 
        // Iterator pointing to the
        // first entry in the map
        Map.Entry itr
            = mp.pollFirstEntry();
 
        for (Map.Entry it :
             mp.entrySet())
        {
 
            // Check if index2 is not greater than index1
            // then we have to perform a concatenation.
            if (itr.getValue() >= it.getValue())
                ans++;
 
            itr = it;
        }
 
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int a[] = { 2, 1, 4, 3, 5 };
 
        int n = a.length;
 
        increasingSubseq(a, n);
    }
}


Python3
# Python 3 implementation to
# Find the minimum concatenation
# required to get strictly Longest
# Increasing Subsequence for the
# given array
def increasingSubseq(a, n):
   
    # Ordered map containing pairs
    # of value and index.
    mp = {}
 
    for i in range(n):
        # Form pairs of value at
        # index i and it's index
        # that is i.
        mp[a[i]] = i
 
    # Variable to insert the
    # minimum concatenations
    # that are needed
    ans = 1
 
    # Iterator pointing to the
    # second pair in the map
    itr = 1
    li= list(mp.values())
    # Iterator pointing to the
    # first pair in the map
    for key, value in mp.items():
       
        # Check if itr tends to
        # end of map
        if (itr < len(mp)):
           
            # Check if index2 is not
            # greater than index1
            # then we have to perform
            # a concatenation.
            if (li[itr] <= key):
                ans += 1
                 
            itr+=1
 
    # Return the final
    # answer
     
    print(ans)
 
# Driver code
if __name__ == '__main__':
   
    a = [2, 1, 4, 3, 5]
    n = len(a)
    increasingSubseq(a, n)
 
# This code is contributed by bgangwar59


输出
3

时间复杂度: O(N * log N)