Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LIS2
- {
- class Program
- {
- public static void Main()
- {
- List<int> sequence = Console.ReadLine().Split().Select(int.Parse).ToList();
- int[] len = new int[sequence.Count];
- int[] prev = new int[sequence.Count];
- List<int> longestSeq = FindLongestIncreasingSubsequence(sequence, len, prev);
- Console.WriteLine( string.Join(" ", longestSeq));
- }
- public static List<int> FindLongestIncreasingSubsequence(List<int> sequence, int[] len, int[] prev)
- {
- int maxLen = 0;
- int maxIndex = 0;
- for (int current = 0; current < sequence.Count; current++)
- {
- len[current] = 1;
- prev[current] = -1;
- for (int i = 0; i < current; i++)
- {
- if (sequence[current] > sequence[i] && len[i] + 1 >len[current])
- {
- len[current] = len[i] + 1;
- prev[current] = i;
- }
- }
- if (len[current] > maxLen)
- {
- maxLen = len[current];
- maxIndex = current;
- }
- }
- List<int> lis = new List<int>();
- while (maxIndex != -1)
- {
- lis.Add(sequence[maxIndex]);
- maxIndex = prev[maxIndex];
- }
- lis.Reverse();
- return lis;
- }
- }
- }
Add Comment
Please, Sign In to add comment