using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; /*Problem 6. * Longest Non-Decreasing Sequence Write a program that reads a sequence of integers and finds in it the longest non-decreasing subsequence. In other words, you should remove a minimal number of numbers from the starting sequence, so that the resulting sequence is non-decreasing. In case of several longest non-decreasing sequences, print the leftmost of them. The input and output should consist of a single line, holding integer numbers separated by a space. */ /*https://www.youtube.com/watch?v=Sk0PX0YSHtk */ class Program { static void Main() { string numberStr = Console.ReadLine();// enter numers on one line separated by space char[] split = new char[] { ' ', ',', ';' };//split by string[] spliedNum = numberStr.Split(split, StringSplitOptions.RemoveEmptyEntries); int[] numbers = new int[spliedNum.Length]; int n = numbers.Length;// how many numbers we have for (int i = 0; i < n; i++) { if (int.TryParse(spliedNum[i], out numbers[i])) numbers[i] = int.Parse(spliedNum[i]); } List numberSeq = new List(); List numberSeqMax = new List(); int maxI = (int)Math.Pow((double)2, n) - 1; // number of combination needed to generate using number as binary for (int i = maxI; i >= 1; i--)// no need to check for zeroes start from last to first fo first true and stop {// combination of numbers we will take or not string numBinStr = Convert.ToString(i, 2).PadLeft(n, '0');// generate combination for (int bit = 0; bit < numBinStr.Length; bit++) {// generate unique List of sequnece combination if (numBinStr[bit] == '1') { numberSeq.Add(numbers[bit]); } } bool isNonDecr = true; List countEncr = new List(); List countEqual = new List(); for (int y = 0; y < numberSeq.Count - 1; y++) { // serch for Encreasing if (numberSeq[y] > numberSeq[y + 1]) {// check and skip search for increasing/ equals isNonDecr = false; } } if (isNonDecr) { for (int y = 0; y < numberSeq.Count - 1; y++) { // serch for equal if (numberSeq[y] == numberSeq[y + 1]) {//1 1 1 2 2 2 - > 1 1 1 or 4 1 4 2 4 3 4 - > 4 4 4 4 countEqual.Add(numberSeq[y]); if (y == numberSeq.Count - 2)// chech before before the last // this if stay inside {// add last elm countEqual.Add(numberSeq[y + 1]); } } else break; } for (int y = 0; y < numberSeq.Count - 1; y++) { // serch for Encreasing if (numberSeq[y] < numberSeq[y + 1]) { // 4 1 1 2 - > 1 2 countEncr.Add(numberSeq[y]); if (y == numberSeq.Count - 2)// chech before before the last // this if stay inside {// add last elm countEncr.Add(numberSeq[y + 1]); } } else break; } } if (isNonDecr && countEncr.Count > numberSeqMax.Count) {// add countEncr // 4 1 1 2 - > 1 2 numberSeqMax.Clear(); numberSeqMax = countEncr.ToList(); numberSeq.Clear(); countEncr.Clear(); } else if (isNonDecr && countEqual.Count > numberSeqMax.Count) {//add countEqual //1 1 1 2 2 2 - > 1 1 1 /// 1 4 1 2 1 - > 1 1 1 numberSeqMax.Clear(); numberSeqMax = countEqual.ToList(); numberSeq.Clear(); countEqual.Clear(); } else numberSeq.Clear();//clear sequence } foreach (var item in numberSeqMax) { Console.WriteLine(item); } } }