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;
- using System.Threading.Tasks;
- //You will be given N – an integer. On the next N lines, you will receive sequences of integers, separated by a space.
- //Your task is to add each sequence’s integers to a list, so that it forms an increasing sequence.
- //If there are already elements inside the list, you must find the right-most element, lower by value, than the first element from the //given sequence, and start INSERTING, the sequence’s elements at the position, AFTER the found element. If the increasing sequence is //BROKEN, during the addition of new elements, you must IGNORE the element that breaks it and all others after it.You must also remove //all elements from the list, AFTER the LAST ADDED element. When you process all input lines, you must print the list’s elements, separated by a single space.
- namespace increasing_crisis
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- List<int> result = new List<int>();
- for (int i = 0; i < n; i++)
- {
- List<int> input = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
- bool removeFromResult = false;
- for (int index = 0; index < input.Count; index++)
- {
- if (index > 0)
- {
- if (input[index - 1] > input[index])
- {
- int removeIndex = index;
- input.RemoveRange(removeIndex, input.Count - removeIndex);
- removeFromResult = true;
- break;
- }
- }
- }
- if (i == 0)
- {
- result.AddRange(input);
- }
- else if (i > 0)
- {
- int startInsIndex = (result.FindLastIndex(x => x <= input[0])) + 1;
- for (int index1 = 0; index1 < input.Count; index1++)
- {
- if (removeFromResult && index1 == 0)
- {
- result.RemoveRange(startInsIndex, result.Count - startInsIndex);
- }
- result.Insert(startInsIndex + index1, input[index1]);
- }
- for (int index = 0; index < result.Count; index++)
- {
- if (index > 0)
- {
- if (result[index - 1] > result[index])
- {
- int removeIndex = index;
- result.RemoveRange(removeIndex, result.Count - removeIndex);
- break;
- }
- }
- }
- }
- }
- Console.WriteLine(string.Join(" ", result));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement