Advertisement
angelneychev

Untitled

Jun 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Socks
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var leftSocks = Console.ReadLine()
  12.                 .Split()
  13.                 .Select(int.Parse)
  14.                 .ToArray();
  15.  
  16.             var rightSocks = Console.ReadLine()
  17.                 .Split()
  18.                 .Select(int.Parse)
  19.                 .ToArray();
  20.  
  21.             Stack<int> left = new Stack<int>(leftSocks);
  22.             Queue<int> right = new Queue<int>(rightSocks);
  23.             List<int> pairs = new List<int>();
  24.  
  25.             int pairOfSocks;
  26.  
  27.             while (left.Count > 0 && right.Count > 0)
  28.             {
  29.                 int leftSock = left.Peek();
  30.                 int rightSock = right.Peek();
  31.  
  32.                 if (leftSock > rightSock)
  33.                 {
  34.                     pairOfSocks = rightSock + leftSock;
  35.                     pairs.Add(pairOfSocks);
  36.                     left.Pop();
  37.                     right.Dequeue();
  38.  
  39.                     if (left.Count > 0 && right.Count > 0)
  40.                     {
  41.                         leftSock = left.Peek();
  42.                         rightSock = right.Peek();
  43.                     }
  44.  
  45.                 }
  46.  
  47.                 if (rightSock > leftSock)
  48.                 {
  49.                     left.Pop();
  50.  
  51.                     if (left.Count > 0)
  52.                     {
  53.                         leftSock = left.Peek();
  54.                     }
  55.  
  56.                 }
  57.  
  58.                 if (leftSock == rightSock)
  59.                 {
  60.  
  61.                     rightSock++;
  62.                     left.Pop();
  63.                     left.Push(rightSock);
  64.                     right.Dequeue();
  65.  
  66.                     if (right.Count > 0)
  67.                     {
  68.                         rightSock = right.Peek();
  69.                     }
  70.                 }
  71.             }
  72.  
  73.             int biggestSet = pairs.OrderByDescending(p => p).FirstOrDefault();
  74.  
  75.             Console.WriteLine(biggestSet);
  76.             Console.WriteLine(string.Join(" ", pairs));
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement