Advertisement
GeorgiGG

Untitled

Apr 20th, 2016
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. namespace LargestIncreasingSubsequence
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             List<int> nums = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  15.             int L = 0;
  16.             int newL = 0;
  17.             //imlementing algorithm from https://en.wikipedia.org/wiki/Longest_increasing_subsequence
  18.             int[] p = new int[nums.Count];
  19.             int[] m = new int[nums.Count + 1];
  20.             for (int i = 0; i < nums.Count-1; i++)
  21.             {
  22.                 decimal low = 1.0m;
  23.                 decimal high = L;
  24.                 while (low < high)
  25.                 {
  26.                     int mid = (int)Math.Ceiling((low+high)/2);
  27.                     if (nums[m[mid]] < nums[i])
  28.                     {
  29.                         low = mid + 1;
  30.                     }
  31.                     else
  32.                     {
  33.                         high = mid - 1;
  34.                     }
  35.                 }
  36.                 newL = (int)low;
  37.                 p[i] = m[newL - 1];
  38.                 m[newL] = i;
  39.                 if (newL > L)
  40.                 {
  41.                     L = newL;
  42.                 }
  43.             }
  44.             int[] s = new int[L];
  45.             int k = m[L];
  46.             for (int i = L-1; i < 0; i--)
  47.             {
  48.                 s[i] = nums[k];
  49.                 k = p[k];
  50.             }
  51.             for (int i = 0; i < L; i++)
  52.             {
  53.                 Console.WriteLine(s[i]);
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement