Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2019
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. namespace P05_LongestIncreasingSubsequence
  2. {
  3.     using System;
  4.     using System.Linq;
  5.  
  6.     class P05_LongestIncreasingSubsequence
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] nums = Console.ReadLine()
  11.                                 .Split()
  12.                                 .Select(int.Parse)
  13.                                 .ToArray();
  14.             int maxLength = 0;
  15.             int lastIndex = -1;
  16.             int[] len = new int[nums.Length];
  17.             int[] prev = new int[nums.Length];
  18.  
  19.             for (int i = 0; i < nums.Length; i++)
  20.             {
  21.                 len[i] = 1;
  22.                 prev[i] = -1;
  23.  
  24.                 for (int k = 0; k < i; k++)
  25.                 {
  26.                     if (nums[k] < nums[i] && len[k] + 1 > len[i])
  27.                     {
  28.                         len[i] = len[k] + 1;
  29.                         prev[i] = k;
  30.                     }
  31.                 }
  32.  
  33.                 if (len[i] > maxLength)
  34.                 {
  35.                     maxLength = len[i];
  36.                     lastIndex = i;
  37.                 }
  38.             }
  39.  
  40.             int[] LIS = new int[maxLength];
  41.             int currentIndex = maxLength - 1;
  42.  
  43.             while (lastIndex != -1)
  44.             {
  45.                 LIS[currentIndex] = nums[lastIndex];
  46.                 currentIndex--;
  47.                 lastIndex = prev[lastIndex];
  48.             }
  49.  
  50.             Console.WriteLine(string.Join(" ", LIS));
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement