Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Socks
- {
- class Program
- {
- static void Main(string[] args)
- {
- var leftSocks = Console.ReadLine()
- .Split()
- .Select(int.Parse)
- .ToArray();
- var rightSocks = Console.ReadLine()
- .Split()
- .Select(int.Parse)
- .ToArray();
- Stack<int> left = new Stack<int>(leftSocks);
- Queue<int> right = new Queue<int>(rightSocks);
- List<int> pairs = new List<int>();
- int pairOfSocks;
- while (left.Count > 0 && right.Count > 0)
- {
- int leftSock = left.Peek();
- int rightSock = right.Peek();
- if (leftSock > rightSock)
- {
- pairOfSocks = rightSock + leftSock;
- pairs.Add(pairOfSocks);
- left.Pop();
- right.Dequeue();
- if (left.Count > 0 && right.Count > 0)
- {
- leftSock = left.Peek();
- rightSock = right.Peek();
- }
- }
- if (rightSock > leftSock)
- {
- left.Pop();
- if (left.Count > 0)
- {
- leftSock = left.Peek();
- }
- }
- if (leftSock == rightSock)
- {
- rightSock++;
- left.Pop();
- left.Push(rightSock);
- right.Dequeue();
- if (right.Count > 0)
- {
- rightSock = right.Peek();
- }
- }
- }
- int biggestSet = pairs.OrderByDescending(p => p).FirstOrDefault();
- Console.WriteLine(biggestSet);
- Console.WriteLine(string.Join(" ", pairs));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement