Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. public class Program
  5. {
  6. public static List<KittenNode> nodes = new List<KittenNode>();
  7. public static List<KittenNode> nodesStartTime = new List<KittenNode>();
  8. public static List<KittenNode> result = new List<KittenNode>();
  9. public static void Main()
  10. {
  11. nodes = new List<KittenNode>();
  12.  
  13. //Accept input for kittens and beds.
  14. string[] startingConditions = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.None);
  15. int kittens = int.Parse(startingConditions[0]), beds = int.Parse(startingConditions[1]);
  16.  
  17. //Initialize node list
  18. for (int i = 0; i < kittens; i++)
  19. {
  20. string[] demand = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.None);
  21. KittenNode newNode = new KittenNode(int.Parse(demand[0]), int.Parse(demand[1]), null);
  22. nodes.Add(newNode);
  23. }
  24. //Sort nodes by ascending exit times
  25. nodes = nodes.OrderBy(o => o.exitTime).ToList();
  26. //Sort nodesStartTime by ascending start times
  27. nodesStartTime = nodes.OrderBy(o => o.arrivalTime).ToList();
  28.  
  29. CacheIntersectionResults();
  30.  
  31. int catCount = 0;
  32. //Count the cats that fit into each bed and add them together.
  33. for (int i = 0; i < beds; i++)
  34. {
  35. KittenNode startingNode = FindOptimalNode(nodes, nodesStartTime, beds - i);
  36. catCount += CountCats(startingNode, beds - i);
  37. }
  38. //Output amount of cats.
  39. Console.WriteLine(catCount);
  40. Console.ReadLine();
  41. }
  42.  
  43.  
  44. public static int CountCats(KittenNode startingNode, int consideredNodeCount)
  45. {
  46. //If no cats are left in nodes, return.
  47. if (startingNode.exitTime == int.MaxValue)
  48. return 0; //No more cats
  49.  
  50. //If more cats exist in nodes, find optimal node path for this bed.
  51. FindOptimalNodePath(startingNode, new List<KittenNode>(nodes), new List<KittenNode>(nodesStartTime), consideredNodeCount);
  52.  
  53. //Count the cats and remove them from 'nodes'.
  54. int catCount = result.Count;
  55. foreach (KittenNode resultNode in result)
  56. {
  57. nodes.Remove(resultNode);
  58. nodesStartTime.Remove(resultNode);
  59. //foreach (KittenNode node in nodes) // K * N^2
  60. //{
  61. // if (node.intersections != null && node.intersections.Contains(resultNode))
  62. // node.intersections.Remove(resultNode);
  63. //}
  64. }
  65. result.Clear();
  66. return catCount;
  67. }
  68. public static void FindOptimalNodePath(KittenNode current, List<KittenNode> tempNodes, List<KittenNode> tempNodesStartTime, int consideredNodeCount)
  69. {
  70. //Remove current node from consideration.
  71. tempNodes.Remove(current);
  72. tempNodesStartTime.Remove(current);
  73. //Add current node to result.
  74. result.Add(current);
  75.  
  76. // Find and remove all unreachable nodes
  77. List<KittenNode> removeList = new List<KittenNode>();
  78. for (int i = 0; i < tempNodes.Count; i++)
  79. if (tempNodes[i].arrivalTime < current.exitTime)
  80. removeList.Add(tempNodes[i]);
  81. foreach (KittenNode node in removeList)
  82. {
  83. tempNodes.Remove(node);
  84. tempNodesStartTime.Remove(node);
  85. }
  86.  
  87. //If no more potential nodes exist, go to next loop or exit.
  88. if (tempNodes.Count == 0)
  89. return;
  90. //If more potential nodes exist, keep searching for optimal path.
  91. else
  92. {
  93. current = FindOptimalNode(tempNodes, tempNodesStartTime, consideredNodeCount); //K^2
  94. FindOptimalNodePath(current, tempNodes, tempNodesStartTime, consideredNodeCount); //K*N*Log(N)
  95. }
  96. }
  97.  
  98. public static void CacheIntersectionResults()
  99. {
  100. //For all considered nodes, count the amount of intersections for each.
  101. //Intersection is defined by the starting point of a given node falling between the starting point and end point of a considered node.
  102. for (int i = 0; i < nodes.Count; i++)
  103. {
  104. nodes[i].intersections = new List<KittenNode>();
  105. for (int j = 0; j < nodesStartTime.Count; j++)
  106. {
  107. if (nodes[i].arrivalTime < nodesStartTime[j].arrivalTime && nodes[i].exitTime > nodesStartTime[j].arrivalTime)
  108. nodes[i].intersections.Add(nodesStartTime[j]);
  109. else if (nodes[i].exitTime < nodesStartTime[j].arrivalTime)
  110. break;
  111. }
  112. }
  113. }
  114.  
  115. public static KittenNode FindOptimalNode(List<KittenNode> nodes, List<KittenNode> nodesStartTime, int consideredNodeCount)
  116. {
  117. //If no more nodes exist, return invalid node.
  118. if (nodes.Count == 0)
  119. return new KittenNode(0, int.MaxValue, null);
  120. //If amount of beds exceed amount of kittens or only one bed is available, return the lowest exit time.
  121. else if (nodes.Count < consideredNodeCount || consideredNodeCount == 1)
  122. return nodes[0];
  123.  
  124. KittenNode currentNode = new KittenNode(0, int.MaxValue, null);
  125. int highestIntersectionCount = -1;
  126. //Iterate through intersections to find the highest amount.
  127. for (int i = 0; i < consideredNodeCount; i++)
  128. if (nodes[i].intersections.Count > highestIntersectionCount)
  129. {
  130. highestIntersectionCount = nodes[i].intersections.Count;
  131. //If two nodes have the same amount of intersections, select the one with lowest exit time. (Happens automatically due to sorting of 'nodes' list)
  132. currentNode = nodes[i];
  133. }
  134.  
  135. return currentNode;
  136. }
  137.  
  138. public class KittenNode
  139. {
  140. public KittenNode(int arrivalTime, int exitTime, List<KittenNode> intersections)
  141. {
  142. this.arrivalTime = arrivalTime;
  143. this.exitTime = exitTime;
  144. this.intersections = intersections;
  145. }
  146. public List<KittenNode> intersections;
  147. public int arrivalTime, exitTime;
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement