Advertisement
Guest User

LongestNonDecreasingSubsequence

a guest
Apr 5th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class LongestNonDecreasingSubsequence
  5. {
  6.  
  7.     static void Main()
  8.     {
  9.         string input = Console.ReadLine();
  10.         string[] numbers = input.Split(' '.ToString().ToCharArray(),
  11.                                         StringSplitOptions.RemoveEmptyEntries);
  12.  
  13.  
  14.         int[] nums = new int[numbers.Length];
  15.         int max = 0, lastIndex = 0, count = 0, constMax = 0;
  16.  
  17.         List<int> maxCount = new List<int>();      
  18.         List<int> constMaxCount = new List<int>();
  19.  
  20.         for (int i = 0; i < nums.Length; i++)
  21.         {
  22.             nums[i] = int.Parse(numbers[i]);
  23.         }
  24.  
  25.         for (int i = 0; i < nums.Length-1; i++)
  26.         {
  27.             max = 0;
  28.             maxCount = new List<int>();
  29.             maxCount.Add(nums[i]);
  30.             count = 0;
  31.             lastIndex = i;
  32.  
  33.             for (int j = i; j < nums.Length - 1; j++)
  34.             {
  35.                     if (nums[lastIndex] <= nums[lastIndex + 1])
  36.                     {
  37.                         max++;
  38.                         maxCount.Add(nums[lastIndex + 1]);
  39.                         count++;
  40.                         lastIndex++;
  41.                     }
  42.  
  43.                     else if (count > 0)
  44.                     {
  45.                         if (maxCount[count] > nums[j] && maxCount[count - 1] < nums[j])
  46.                         {
  47.                             maxCount[count] = nums[j];
  48.                             lastIndex=j;
  49.                         }
  50.                     }
  51.  
  52.                 }
  53.  
  54.                 if (max > constMax)
  55.                 {
  56.                     constMax = max;
  57.                     constMaxCount = maxCount;
  58.                 }
  59.  
  60.             }
  61.  
  62.         if (nums[nums.Length - 1] > nums[nums.Length - 2])
  63.             {
  64.                 constMaxCount.Add(nums[nums.Length - 1]);
  65.             }
  66.  
  67.         foreach (var number in constMaxCount)
  68.         {
  69.             Console.Write("{0} ",number);
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement