Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.70 KB | None | 0 0
  1. namespace Sticks
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public static class Startup
  8.     {
  9.         private static string HandleInput(Func<string> inputFunc)
  10.         {
  11.             string input = inputFunc.Invoke();
  12.  
  13.             if (string.IsNullOrEmpty(input) || string.IsNullOrWhiteSpace(input))
  14.             {
  15.                 throw new ArgumentException("Input cannot be empty!", "inputFunc");
  16.             }
  17.  
  18.             return input;
  19.         }
  20.  
  21.         public static void Main()
  22.         {
  23.             int sticksCount = int.Parse(HandleInput(Console.ReadLine));
  24.            
  25.             ICollection<int> sticks = new HashSet<int>();
  26.  
  27.             for (int index = 0; index < sticksCount; index++)
  28.             {
  29.                 sticks.Add(index);
  30.             }
  31.  
  32.             int placings = int.Parse(HandleInput(Console.ReadLine));
  33.  
  34.             ICollection<int> upperSticks = new HashSet<int>();
  35.             ICollection<int> lowerSticks = new HashSet<int>();
  36.  
  37.             IDictionary<int, ICollection<int>> lowerSticksByUpperStick = new Dictionary<int, ICollection<int>>();
  38.             IDictionary<int, ICollection<int>> upperSticksByLowerStick = new Dictionary<int, ICollection<int>>();
  39.  
  40.             for (int i = 0; i < placings; i++)
  41.             {
  42.                 string input = HandleInput(Console.ReadLine);
  43.                 string[] inputArgs = input.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  44.  
  45.                 int upperStick = int.Parse(inputArgs[0]);
  46.                 int lowerStick = int.Parse(inputArgs[1]);
  47.  
  48.                 upperSticks.Add(upperStick);
  49.                 lowerSticks.Add(lowerStick);
  50.  
  51.                 if (lowerSticksByUpperStick.ContainsKey(upperStick))
  52.                 {
  53.                     lowerSticksByUpperStick[upperStick].Add(lowerStick);
  54.                 }
  55.                 else
  56.                 {
  57.                     lowerSticksByUpperStick[upperStick] = new HashSet<int> { lowerStick };
  58.                 }
  59.  
  60.                 if (upperSticksByLowerStick.ContainsKey(lowerStick))
  61.                 {
  62.                     upperSticksByLowerStick[lowerStick].Add(upperStick);
  63.                 }
  64.                 else
  65.                 {
  66.                     upperSticksByLowerStick[lowerStick] = new HashSet<int> { upperStick };
  67.                 }
  68.             }
  69.  
  70.             ICollection<int> upmostSticks = GetUpmostSticks(sticks, upperSticks, lowerSticks, new HashSet<int>());
  71.             ICollection<int> usedSticks = new HashSet<int>();
  72.  
  73.             while (upmostSticks.Any())
  74.             {
  75.                 // take the upmost stick
  76.                 int upmostStick = upmostSticks.FirstOrDefault();
  77.  
  78.                 // remove it
  79.                 upperSticks.Remove(upmostStick);
  80.  
  81.                 if (lowerSticksByUpperStick.ContainsKey(upmostStick))
  82.                 {
  83.                     ICollection<int> lowerSticksCollection = lowerSticksByUpperStick[upmostStick];
  84.  
  85.                     foreach (int stick in lowerSticksCollection)
  86.                     {
  87.                         upperSticksByLowerStick[stick].Remove(upmostStick);
  88.  
  89.                         if (!upperSticksByLowerStick[stick].Any())
  90.                         {
  91.                             lowerSticks.Remove(stick);
  92.                         }
  93.                     }
  94.  
  95.                     lowerSticksByUpperStick.Remove(upmostStick);
  96.                 }
  97.  
  98.                 // mark as used
  99.                 usedSticks.Add(upmostStick);
  100.  
  101.                 upmostSticks = GetUpmostSticks(sticks, upperSticks, lowerSticks, usedSticks);
  102.             }
  103.  
  104.             if (usedSticks.Count == sticks.Count)
  105.             {
  106.                 Console.WriteLine(string.Join(" ", usedSticks));
  107.             }
  108.             else
  109.             {
  110.                 Console.WriteLine("Cannot lift all sticks\n{0}", string.Join(" ", usedSticks));
  111.             }
  112.         }
  113.  
  114.         private static ICollection<int> GetUpmostSticks(
  115.             ICollection<int> sticks, ICollection<int> upperSticks, ICollection<int> lowerSticks, ICollection<int> usedSticks)
  116.         {
  117.             var resultingStack = new Stack<int>();
  118.  
  119.             foreach (int stick in sticks)
  120.             {
  121.                 if (upperSticks.Contains(stick) && !lowerSticks.Contains(stick) ||
  122.                     !upperSticks.Contains(stick) && !lowerSticks.Contains(stick) && !usedSticks.Contains(stick))
  123.                 {
  124.                     resultingStack.Push(stick);
  125.                 }
  126.             }
  127.  
  128.             var result = new HashSet<int>();
  129.  
  130.             while (resultingStack.Any())
  131.             {
  132.                 result.Add(resultingStack.Pop());
  133.             }
  134.  
  135.             return result;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement