Advertisement
Guest User

13. Longest Increasing Subsequence

a guest
Oct 17th, 2016
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class LongestIncreasingSequence
  6. {
  7. static void Main()
  8. {
  9. int[] input = Console.ReadLine().Trim().Split()
  10. .Select(int.Parse).ToArray();
  11.  
  12. List<int> temp = new List<int>();
  13. List<int> result = new List<int>();
  14.  
  15.  
  16. temp.Add(input[0]);
  17. for (int i = 1; i < input.Length; i++)
  18. {
  19. if (input[i] > input[i - 1])
  20. {
  21. temp.Add(input[i]);
  22. if (i == input.Length - 1)
  23. {
  24. Console.WriteLine(string.Join(" ", temp));
  25. if (temp.Count > result.Count)
  26. {
  27. result.Clear();
  28. result.InsertRange(0, temp);
  29. }
  30. }
  31. }
  32. else
  33. {
  34. Console.WriteLine(string.Join(" ", temp));
  35. if (temp.Count > result.Count)
  36. {
  37. result.Clear();
  38. result.InsertRange(0, temp);
  39. }
  40. temp.Clear();
  41. temp.Add(input[i]);
  42. if (i == input.Length - 1)
  43. {
  44. Console.WriteLine(string.Join(" ", temp));
  45. }
  46.  
  47. }
  48. }
  49. Console.WriteLine("Longest: {0}", string.Join(" ", result));
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement