fork download
  1. /*
  2. APPLE AND ORANGE
  3.  
  4. Sam's house has an apple tree and an orange tree that yield an abundance of fruit. In the diagram below, the red region denotes his house, where s is the start point and t is the end point. The apple tree is to the left of his house, and the orange tree is to its right. You can assume the trees are located on a single point, where the apple tree is at point a and the orange tree is at point b.
  5.  
  6. Apple and orange(2).png
  7.  
  8. When a fruit falls from its tree, it lands d units of distance from its tree of origin along the x-axis. A negative value of d means the fruit fell d units to the tree's left, and a positive value of d means it falls d units to the tree's right.
  9.  
  10. Given the value of d for m apples and n oranges, can you determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range [s,t])? Print the number of apples that fall on Sam's house as your first line of output, then print the number of oranges that fall on Sam's house as your second line of output.
  11.  
  12. Input Format
  13.  
  14. The first line contains two space-separated integers denoting the respective values of s and t.
  15. The second line contains two space-separated integers denoting the respective values of a and b.
  16. The third line contains two space-separated integers denoting the respective values of m and n.
  17. The fourth line contains m space-separated integers denoting the respective distances that each apple falls from point a.
  18. The fifth line contains n space-separated integers denoting the respective distances that each orange falls from point b.
  19.  
  20. Constraints
  21.   * 1 <= s, t, a, b, m, n <= 10^5
  22.   * -10^5 <= d <= 10^5
  23.   * a < s < t < b
  24.  
  25. Output Format
  26.  
  27. Print two lines of output:
  28.  
  29. 1 On the first line, print the number of apples that fall on Sam's house.
  30. 2 On the second line, print the number of oranges that fall on Sam's house.
  31.  
  32. Sample Input 0
  33. 7 11
  34. 5 15
  35. 3 2
  36. -2 2 1
  37. 5 -6
  38.  
  39. Sample Output 0
  40. 1
  41. 1
  42.  
  43. Explanation 0
  44. The first apple falls at position 5 - 2 = 3.
  45. The second apple falls at position 5 + 2 = 7.
  46. The third apple falls at position 5 + 1 = 8.
  47. The first orange falls at position 15 + 5 = 20.
  48. The second orange falls at position 15 - 6 = 11.
  49. Only one fruit (the second apple) falls within the region between 7 and 11, so we print 1 as our first line of output.
  50. Only the second orange falls within the region between 7 and 11, so we print 1 as our second line of output.
  51.  
  52. */
  53. using System;
  54. using System.Collections.Generic;
  55. using System.IO;
  56. using System.Linq;
  57. class Solution {
  58. public static class House{
  59. // Static Constructor
  60. static House(){ // Initializes variables and create instances of Tree.
  61. Start = 0; // Initial value of Start
  62. End = 0; // Initial value of End
  63. AQueries = 0; // Initial value of Apple queries
  64. OQueries = 0; // Initial value of Orange queries
  65. Apple = new Tree(); // Creating an instance of the Apple Tree.
  66. Orange = new Tree(); // Creating an instance of the Orange Tree.
  67. }
  68.  
  69. // Properties
  70. public static int Start{ get; set; } // Starting position of the house.
  71. public static int End{ get; set; } // Ending position of the house.
  72. public static int AQueries { get; set; } // Number of Apple queries
  73. public static int OQueries { get; set; } // Number of Orange queries
  74. public static Tree Apple { get; set; } // Apple Tree
  75. public static Tree Orange { get; set; } // Orange Tree
  76.  
  77. // Method
  78. public static void display(){ // Calls the display methods for each Tree.
  79. Apple.display(Start, End);
  80. Orange.display(Start, End);
  81. }
  82. }
  83.  
  84. public class Tree{
  85. // Private variable
  86. private List<int> _lintFruit; // Contains the positions of each fruit.
  87.  
  88. // Constructor
  89. public Tree(){
  90. X = 0; // Starting position of the Tree.
  91. _lintFruit = new List<int>(); // Initializes the list.
  92. }
  93.  
  94. // Property
  95. public int X{ get; set; } // Position of the Tree.
  96.  
  97. // Methods
  98. public void setFruit(int[] value) { // Takes the int array and stores it in a list.
  99. foreach(int val in value){
  100. _lintFruit.Add(val + X); // Adds the Tree's position to the value to find the fruit's position.
  101. }
  102. }
  103.  
  104. public void display(int start, int end){
  105. Console.WriteLine("{0}", _lintFruit.Where(y => y >= start && y <= end).Count()); // Prints the number of fruit that is within the range.
  106. }
  107. }
  108.  
  109. static void Main(String[] args) {
  110. /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
  111. IInput input;
  112.  
  113. if (args.Length > 0){
  114. input = new StringInput(System.IO.File.ReadAllLines(args[0]));
  115. } else {
  116. input = new ConsoleInput();
  117. }
  118.  
  119. int[] values = input.ReadIntArray();
  120. House.Start = values[0];
  121. House.End = values[1];
  122. values = input.ReadIntArray();
  123. House.Apple.X = values[0];
  124. House.Orange.X = values[1];
  125. values = input.ReadIntArray();
  126. House.AQueries = values[0];
  127. House.OQueries = values[1];
  128. House.Apple.setFruit(input.ReadIntArray());
  129. House.Orange.setFruit(input.ReadIntArray());
  130. House.display();
  131. }
  132.  
  133. public interface IInput{
  134. string ReadLine();
  135. int ReadInt();
  136. int[] ReadIntArray();
  137. }
  138.  
  139. public class ConsoleInput : IInput{
  140. public string ReadLine(){
  141. return Console.ReadLine().Trim();
  142. }
  143.  
  144. public int ReadInt(){
  145. return Convert.ToInt32(ReadLine());
  146. }
  147.  
  148. public int[] ReadIntArray(){
  149. return Array.ConvertAll(ReadLine().Split(' '), Int32.Parse);
  150. }
  151. }
  152.  
  153. public class StringInput : IInput{
  154. IEnumerable<string> _data;
  155. IEnumerator<string> _dataEnumerator;
  156.  
  157. public StringInput (IEnumerable<string> data){
  158. _data = data;
  159. _dataEnumerator = _data.GetEnumerator();
  160. }
  161.  
  162. public string ReadLine(){
  163. _dataEnumerator.MoveNext();
  164. string retVal = _dataEnumerator.Current.Trim();
  165. return retVal;
  166. }
  167.  
  168. public int ReadInt(){
  169. return Convert.ToInt32(ReadLine());
  170. }
  171.  
  172. public int[] ReadIntArray(){
  173. return Array.ConvertAll(ReadLine().Split(' '), Int32.Parse);
  174. }
  175. }
  176. }
Success #stdin #stdout 0.01s 132160KB
stdin
7 11
5 15
3 2
-2 2 1
5 -6
stdout
1
1