Advertisement
Guest User

Untitled

a guest
Sep 29th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _05_IncreasingCrisis
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             List<int> mainSequence = new List<int>();
  13.  
  14.             for (int i = 0; i < n; i++)
  15.             {
  16.                 int[] newSequence = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  17.  
  18.                 InsertSequence(mainSequence, newSequence);
  19.             }
  20.            
  21.             Console.WriteLine(string.Join(" ", mainSequence));
  22.         }
  23.        
  24.         static void InsertSequence(List<int> mainList, int[] toInsert)
  25.         {
  26.             int lowerIndex = FindLowerElement(mainList, toInsert[0]);
  27.  
  28.             if (lowerIndex < 0) return;
  29.  
  30.             for (int i = 0; i < toInsert.Length; i++)
  31.             {
  32.                 int elementToInsert = toInsert[i];
  33.                 int insertIndex = lowerIndex + i + 1;
  34.  
  35.                 if (i > 0)
  36.                 {
  37.                     if (elementToInsert < toInsert[i - 1])
  38.                     {
  39.                         RemoveAfter(mainList, insertIndex);
  40.                         return;
  41.                     }
  42.                 }
  43.                
  44.                 if(insertIndex >= mainList.Count)
  45.                 {
  46.                     mainList.Add(elementToInsert);
  47.                 }
  48.                 else if (elementToInsert <= mainList[insertIndex])
  49.                 {
  50.                     mainList.Insert(insertIndex, elementToInsert);
  51.                 }
  52.                 else
  53.                 {
  54.                     mainList.Insert(insertIndex, elementToInsert);  // this is for 100/100 judge :(
  55.                     RemoveAfter(mainList, insertIndex + 1);
  56.                     return;
  57.                 }
  58.                 /*{ // "correct" solution but gives 80/100 in judge!
  59.                     RemoveAfter(mainList, insertIndex);
  60.                     return;
  61.                 }*/
  62.             }
  63.         }
  64.  
  65.         static int FindLowerElement(List<int> list, int element)
  66.         {
  67.             if (list.Count == 0) return 0;
  68.  
  69.             for (int i = list.Count - 1; i >= 0; i--)
  70.             {
  71.                 if (list[i] <= element) return i;
  72.             }
  73.  
  74.             return -1;
  75.         }
  76.  
  77.         static void RemoveAfter(List<int> list, int index)
  78.         {
  79.             while (index < list.Count)
  80.             {
  81.                 list.RemoveAt(index);
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement