Advertisement
grubcho

Increasing crisis

Jul 5th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //You will be given N – an integer. On the next N lines, you will receive sequences of integers, separated by a space.
  7. //Your task is to add each sequence’s integers to a list, so that it forms an increasing sequence.
  8. //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.
  9. namespace increasing_crisis
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int n = int.Parse(Console.ReadLine());
  16.             List<int> result = new List<int>();
  17.             for (int i = 0; i < n; i++)
  18.             {
  19.                 List<int> input = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  20.                 bool removeFromResult = false;
  21.                 for (int index = 0; index < input.Count; index++)
  22.                 {
  23.                     if (index > 0)
  24.                     {
  25.                         if (input[index - 1] > input[index])
  26.                         {
  27.                             int removeIndex = index;
  28.                             input.RemoveRange(removeIndex, input.Count - removeIndex);
  29.                             removeFromResult = true;
  30.                             break;
  31.                         }
  32.                     }
  33.                 }
  34.                 if (i == 0)
  35.                 {
  36.                     result.AddRange(input);
  37.                 }
  38.                 else if (i > 0)
  39.                 {
  40.                     int startInsIndex = (result.FindLastIndex(x => x <= input[0])) + 1;
  41.                     for (int index1 = 0; index1 < input.Count; index1++)
  42.                     {
  43.                         if (removeFromResult && index1 == 0)
  44.                         {
  45.                             result.RemoveRange(startInsIndex, result.Count - startInsIndex);
  46.                         }
  47.                         result.Insert(startInsIndex + index1, input[index1]);
  48.                     }
  49.                     for (int index = 0; index < result.Count; index++)
  50.                     {
  51.                         if (index > 0)
  52.                         {
  53.                             if (result[index - 1] > result[index])
  54.                             {
  55.                                 int removeIndex = index;
  56.                                 result.RemoveRange(removeIndex, result.Count - removeIndex);
  57.                                 break;
  58.                             }
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.             Console.WriteLine(string.Join(" ", result));
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement