Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Problem 5. * Longest Non-Decreasing Subsequence
- //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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class Test
- {
- static void Main()
- {
- var input = Console.ReadLine().Split().Select(int.Parse).ToArray();
- var resultList = new List<int>();
- int lenght = 0;
- string str = "";
- string sequence = "";
- int combinations = (int)Math.Pow(2, (input.Length));
- Console.WriteLine();
- for(int i = 1; i <= combinations; i++)
- {
- int[] bits = new int[input.Length];
- for (int k = 0; k < input.Length; k ++)
- {
- int temp = 0;
- bits[k] = (i >> k) & 1;
- temp = bits[k] * input[k];
- if (temp != 0)
- {
- resultList.Add(temp);
- }
- }
- int num = 0;
- int currentcount = 0;
- for(int j = 0; j < resultList.Count; j++)
- {
- if (resultList[j] >= num)
- {
- str += resultList[j] + " ";
- currentcount++;
- num = resultList[j];
- }
- }
- if (currentcount > lenght)
- {
- sequence = str;
- lenght = currentcount;
- }
- resultList.Clear();
- str = string.Empty;
- }
- Console.WriteLine(sequence);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment