fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace MaximumN
  6. {
  7. internal class Program
  8. {
  9. private static void Main()
  10. {
  11. const int n = 100000;
  12. var arr = new double[n];
  13. var random = new Random();
  14. for(int i=0; i<n; i++)
  15. {
  16. arr[i] = random.NextDouble();
  17. }
  18. foreach (double a in arr.GetMaxElements().Take(10))
  19. {
  20. Console.WriteLine(a);
  21. }
  22. Console.ReadKey();
  23. }
  24. }
  25.  
  26. internal static class CustomExtensions
  27. {
  28. public static IEnumerable<double> GetMaxElements(
  29. this IEnumerable<double> source
  30. )
  31. {
  32. var usedIndices = new HashSet<int>();
  33. int count = source.Count();
  34. while (usedIndices.Count < count)
  35. {
  36. var enumerator = source.GetEnumerator();
  37. int index = 0;
  38. int maxIndex = 0;
  39. double maxValue = Double.NegativeInfinity;
  40. while(enumerator.MoveNext())
  41. {
  42. if(enumerator.Current>maxValue&&!usedIndices.Contains(index))
  43. {
  44. maxValue = enumerator.Current;
  45. maxIndex = index;
  46. }
  47. index++;
  48. }
  49. usedIndices.Add(maxIndex);
  50. yield return maxValue;
  51. }
  52. }
  53. }
  54. }
Success #stdin #stdout 0.08s 34832KB
stdin
Standard input is empty
stdout
0.999974711332458
0.999963931739314
0.999950621742732
0.999940017703893
0.999934924766391
0.999928940087524
0.999924718867952
0.999922224786096
0.999905630014793
0.99990124534811