Advertisement
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;
- class LongestNonDecreasingSubsequence
- {
- static void Main(string[] args)
- {
- string input = "1 1 1 3 3 3 2 2 2 2";
- List<int> lnds = input.Split(' ').ToList().Select(x => int.Parse(x)).ToList();
- List<List<int>> activeLists = new List<List<int>>();
- activeLists.Add(new List<int> { lnds.ElementAt(0) });
- int maxLenght = 0;
- for (int currentElement = 1; currentElement < lnds.Count; currentElement++)
- {
- int c = lnds.ElementAt(currentElement);
- bool isSmallest = true;
- int count = activeLists.Count;
- for (int i = 0; i < count; i++)
- {
- if (c >= activeLists.ElementAt(i).Last())
- {
- List<int> temp = CloneList(activeLists.ElementAt(i));
- temp.Add(c);
- activeLists.Add(temp);
- isSmallest = false;
- maxLenght = (activeLists.Last().Count > maxLenght) ? activeLists.Last().Count : maxLenght;
- }
- }
- if (isSmallest)
- {
- activeLists.Add(new List<int> { c });
- continue;
- }
- }
- foreach (List<int> x in activeLists)
- {
- if (x.Count == maxLenght)
- {
- foreach (int a in x)
- Console.Write("{0} ", a);
- Console.WriteLine();
- break;
- }
- }
- }
- public static List<int> CloneList(List<int> l)
- {
- List<int> newList = new List<int>();
- l.ForEach(item => newList.Add(item));
- return newList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement