Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _05_IncreasingCrisis
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- List<int> mainSequence = new List<int>();
- for (int i = 0; i < n; i++)
- {
- int[] newSequence = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- InsertSequence(mainSequence, newSequence);
- }
- Console.WriteLine(string.Join(" ", mainSequence));
- }
- static void InsertSequence(List<int> mainList, int[] toInsert)
- {
- int lowerIndex = FindLowerElement(mainList, toInsert[0]);
- if (lowerIndex < 0) return;
- for (int i = 0; i < toInsert.Length; i++)
- {
- int elementToInsert = toInsert[i];
- int insertIndex = lowerIndex + i + 1;
- if (i > 0)
- {
- if (elementToInsert < toInsert[i - 1])
- {
- RemoveAfter(mainList, insertIndex);
- return;
- }
- }
- if(insertIndex >= mainList.Count)
- {
- mainList.Add(elementToInsert);
- }
- else if (elementToInsert <= mainList[insertIndex])
- {
- mainList.Insert(insertIndex, elementToInsert);
- }
- else
- {
- mainList.Insert(insertIndex, elementToInsert); // this is for 100/100 judge :(
- RemoveAfter(mainList, insertIndex + 1);
- return;
- }
- /*{ // "correct" solution but gives 80/100 in judge!
- RemoveAfter(mainList, insertIndex);
- return;
- }*/
- }
- }
- static int FindLowerElement(List<int> list, int element)
- {
- if (list.Count == 0) return 0;
- for (int i = list.Count - 1; i >= 0; i--)
- {
- if (list[i] <= element) return i;
- }
- return -1;
- }
- static void RemoveAfter(List<int> list, int index)
- {
- while (index < list.Count)
- {
- list.RemoveAt(index);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement