BonchoBelutov

LongestIncreasingSubSequence

May 4th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class LongestIncreasingSubSequence
  5. {
  6.     static void Check(int[] arr, int[] indices, int k)
  7.     {
  8.         int count = 1;
  9.         int result = 0;
  10.         for (int i = 0; i < k; i++)
  11.         {
  12.             count++;
  13.             result = arr.Length - count;
  14.             if (arr[indices[i]] > arr[indices[i + 1]])
  15.             {
  16.                 return; //Reversed because of recursion
  17.             }
  18.         }
  19.        for (int i = 0; i <= k; i++) Console.Write(arr[indices[i]] + (i == k ? "\n" : " "));
  20.         Console.WriteLine(result);
  21.         Environment.Exit(0);
  22.     }
  23.  
  24.     static void Combination(int[] arr, int[] indices, int k, int i, int next)
  25.     {
  26.         if (i > k) return;
  27.  
  28.         for (int j = next; j < arr.Length; j++)
  29.         {
  30.             indices[i] = j;
  31.  
  32.             if (i == k) Check(arr, indices, k);
  33.  
  34.             Combination(arr, indices, k, i + 1, j + 1);
  35.         }
  36.     }
  37.  
  38.     static void Main()
  39.     {
  40.         int[] arr = new int[int.Parse(Console.ReadLine())];
  41.         for (int i = 0; i < arr.Length; i++) arr[i] = int.Parse(Console.ReadLine());
  42.  
  43.         int[] indices = new int[arr.Length];
  44.         for (int i = arr.Length - 1; i >= 0; i--) Combination(arr, indices, i, 0, 0);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment