fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. static readonly int NUMROBOTS = 100;
  8. static readonly int ITERATIONS = 200000;
  9. static Random rng = new Random();
  10.  
  11. int FindParking()
  12. {
  13. int free = 100; // # of largest free spot
  14. int count = 0; // Number of robots parked
  15.  
  16. var robots = Enumerable.Range(1, 100).ToList();
  17. // Shuffle list
  18. int n = robots.Count;
  19. while (n > 1)
  20. {
  21. n--;
  22. int k = rng.Next(n + 1);
  23. int value = robots[k];
  24. robots[k] = robots[n];
  25. robots[n] = value;
  26. }
  27.  
  28. while (free > 0)
  29. {
  30. if (free >= robots[count])
  31. free = robots[count]; // Robot found its spot
  32. else
  33. free--; // Spot is filled
  34. count++;
  35. }
  36. return count;
  37. }
  38.  
  39.  
  40. public static void Main()
  41. {
  42. var p = new Test();
  43. int total = 0;
  44. for (int i = 0; i < ITERATIONS; i++)
  45. {
  46. total += p.FindParking();
  47. }
  48. Console.WriteLine("Average robots parked: {0}", (decimal)total / ITERATIONS);
  49. }
  50. }
Success #stdin #stdout 2.6s 34072KB
stdin
Standard input is empty
stdout
Average robots parked: 12.988875000000000000000000000