Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Sticks
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public static class Startup
- {
- private static string HandleInput(Func<string> inputFunc)
- {
- string input = inputFunc.Invoke();
- if (string.IsNullOrEmpty(input) || string.IsNullOrWhiteSpace(input))
- {
- throw new ArgumentException("Input cannot be empty!", "inputFunc");
- }
- return input;
- }
- public static void Main()
- {
- int sticksCount = int.Parse(HandleInput(Console.ReadLine));
- ICollection<int> sticks = new HashSet<int>();
- for (int index = 0; index < sticksCount; index++)
- {
- sticks.Add(index);
- }
- int placings = int.Parse(HandleInput(Console.ReadLine));
- ICollection<int> upperSticks = new HashSet<int>();
- ICollection<int> lowerSticks = new HashSet<int>();
- IDictionary<int, ICollection<int>> lowerSticksByUpperStick = new Dictionary<int, ICollection<int>>();
- IDictionary<int, ICollection<int>> upperSticksByLowerStick = new Dictionary<int, ICollection<int>>();
- for (int i = 0; i < placings; i++)
- {
- string input = HandleInput(Console.ReadLine);
- string[] inputArgs = input.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
- int upperStick = int.Parse(inputArgs[0]);
- int lowerStick = int.Parse(inputArgs[1]);
- upperSticks.Add(upperStick);
- lowerSticks.Add(lowerStick);
- if (lowerSticksByUpperStick.ContainsKey(upperStick))
- {
- lowerSticksByUpperStick[upperStick].Add(lowerStick);
- }
- else
- {
- lowerSticksByUpperStick[upperStick] = new HashSet<int> { lowerStick };
- }
- if (upperSticksByLowerStick.ContainsKey(lowerStick))
- {
- upperSticksByLowerStick[lowerStick].Add(upperStick);
- }
- else
- {
- upperSticksByLowerStick[lowerStick] = new HashSet<int> { upperStick };
- }
- }
- ICollection<int> upmostSticks = GetUpmostSticks(sticks, upperSticks, lowerSticks, new HashSet<int>());
- ICollection<int> usedSticks = new HashSet<int>();
- while (upmostSticks.Any())
- {
- // take the upmost stick
- int upmostStick = upmostSticks.FirstOrDefault();
- // remove it
- upperSticks.Remove(upmostStick);
- if (lowerSticksByUpperStick.ContainsKey(upmostStick))
- {
- ICollection<int> lowerSticksCollection = lowerSticksByUpperStick[upmostStick];
- foreach (int stick in lowerSticksCollection)
- {
- upperSticksByLowerStick[stick].Remove(upmostStick);
- if (!upperSticksByLowerStick[stick].Any())
- {
- lowerSticks.Remove(stick);
- }
- }
- lowerSticksByUpperStick.Remove(upmostStick);
- }
- // mark as used
- usedSticks.Add(upmostStick);
- upmostSticks = GetUpmostSticks(sticks, upperSticks, lowerSticks, usedSticks);
- }
- if (usedSticks.Count == sticks.Count)
- {
- Console.WriteLine(string.Join(" ", usedSticks));
- }
- else
- {
- Console.WriteLine("Cannot lift all sticks\n{0}", string.Join(" ", usedSticks));
- }
- }
- private static ICollection<int> GetUpmostSticks(
- ICollection<int> sticks, ICollection<int> upperSticks, ICollection<int> lowerSticks, ICollection<int> usedSticks)
- {
- var resultingStack = new Stack<int>();
- foreach (int stick in sticks)
- {
- if (upperSticks.Contains(stick) && !lowerSticks.Contains(stick) ||
- !upperSticks.Contains(stick) && !lowerSticks.Contains(stick) && !usedSticks.Contains(stick))
- {
- resultingStack.Push(stick);
- }
- }
- var result = new HashSet<int>();
- while (resultingStack.Any())
- {
- result.Add(resultingStack.Pop());
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement