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;
- namespace _00.TRY_4
- {
- 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> newList = new List<int>();
- newList = GetANewList(newList);
- AddIncreasingElements(newList, i, result);
- }
- Console.WriteLine(String.Join(" ", result));
- }
- static List<int> GetANewList(List<int> newList)
- {
- newList = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
- return newList;
- }
- static List<int> AddIncreasingElements(List<int> newList, int i, List<int> result)
- {
- if(i == 0)
- {
- result.Add(newList[0]);
- for (int index = 1; index < newList.Count; index++)
- {
- if(newList[index - 1] <= newList[index])
- {
- int elementToAdd = newList[index];
- result.Add(elementToAdd);
- }
- else
- {
- break;
- }
- }
- }
- else
- {
- int elementToInsert = newList[0];
- InsertElement(result, elementToInsert);
- for (int index = 1; index < newList.Count; index++)
- {
- if(newList[index - 1] <= newList[index])
- {
- elementToInsert = newList[index];
- InsertElement(result, elementToInsert);
- }
- else
- {
- int lastInserted = elementToInsert;
- RemoveElements(result, lastInserted);
- break;
- }
- }
- }
- return newList;
- }
- static List<int> InsertElement(List<int> result, int elementToInsert)
- {
- bool hasInserted = false;
- for (int index = 0; index < result.Count; index++)
- {
- int currentListElement = result[index];
- if(elementToInsert <= currentListElement)
- {
- result.Insert(index, elementToInsert);
- hasInserted = true;
- break;
- }
- }
- if(!hasInserted)
- {
- result.Add(elementToInsert);
- }
- return result;
- }
- static List<int> RemoveElements(List<int> result, int lastInserted)
- {
- for (int index = 0; index < result.Count; index++)
- {
- int currentListElement = result[index];
- if (lastInserted < currentListElement)
- {
- result.RemoveAt(index);
- index--;
- }
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment