emilia98

Untitled

Jul 6th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 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.  
  7. namespace _00.TRY_4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             List<int> result = new List<int>();
  15.  
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 List<int> newList = new List<int>();
  19.                 newList = GetANewList(newList);
  20.  
  21.                 AddIncreasingElements(newList, i, result);
  22.  
  23.             }
  24.             Console.WriteLine(String.Join(" ", result));
  25.         }
  26.  
  27.         static List<int> GetANewList(List<int> newList)
  28.         {
  29.             newList = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  30.             return newList;
  31.         }
  32.  
  33.         static List<int> AddIncreasingElements(List<int> newList, int i, List<int> result)
  34.         {
  35.             if(i == 0)
  36.             {
  37.                  result.Add(newList[0]);
  38.                  for (int index = 1; index < newList.Count; index++)
  39.                  {
  40.                      if(newList[index - 1] <= newList[index])
  41.                      {
  42.                          int elementToAdd = newList[index];
  43.                          result.Add(elementToAdd);
  44.                      }
  45.                      else
  46.                      {
  47.                          break;
  48.                      }              
  49.                  }
  50.             }
  51.             else
  52.             {
  53.                 int elementToInsert = newList[0];
  54.                 InsertElement(result, elementToInsert);
  55.                 for (int index = 1; index < newList.Count; index++)
  56.                 {
  57.                     if(newList[index - 1] <= newList[index])
  58.                     {
  59.                         elementToInsert = newList[index];
  60.                         InsertElement(result, elementToInsert);
  61.                     }
  62.                     else
  63.                     {
  64.                         int lastInserted = elementToInsert;
  65.                         RemoveElements(result, lastInserted);
  66.                         break;
  67.                     }                    
  68.                 }
  69.             }
  70.             return newList;
  71.         }
  72.  
  73.         static List<int> InsertElement(List<int> result, int elementToInsert)
  74.         {
  75.             bool hasInserted = false;
  76.             for (int index = 0; index < result.Count; index++)
  77.             {
  78.                 int currentListElement = result[index];
  79.                 if(elementToInsert <= currentListElement)
  80.                 {
  81.                     result.Insert(index, elementToInsert);
  82.                     hasInserted = true;
  83.                     break;
  84.                 }              
  85.             }
  86.             if(!hasInserted)
  87.             {
  88.                 result.Add(elementToInsert);
  89.             }
  90.             return result;
  91.         }
  92.  
  93.         static List<int> RemoveElements(List<int> result, int lastInserted)
  94.         {
  95.             for (int index = 0; index < result.Count; index++)
  96.             {
  97.                 int currentListElement = result[index];
  98.                 if (lastInserted < currentListElement)
  99.                 {
  100.                     result.RemoveAt(index);
  101.                     index--;
  102.                 }                
  103.             }
  104.             return result;
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment