fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4. #include <vector>
  5. #include <cmath>
  6. #include <queue>
  7. #include <string>
  8. using namespace std;
  9.  
  10. using ld = double;
  11. using vld = vector<ld>;
  12. using vi = vector<int>;
  13.  
  14. ld base()
  15. {
  16. return atan(ld(2.718281828));
  17. }
  18.  
  19. pair<vld, ld> generate(int n)
  20. {
  21. vi test;
  22. for (; n > 0; n--)
  23. test.push_back(rand() % 10000);
  24.  
  25. vld result;
  26. int64_t sum = 0;
  27. for (auto d : test)
  28. {
  29. result.push_back(base() * d);
  30. sum += d;
  31. }
  32.  
  33. return { result, sum * base() };
  34. }
  35.  
  36. ld sum_naive(vld v)
  37. {
  38. ld result = 0;
  39. for (auto d : v)
  40. result += d;
  41. return result;
  42. }
  43.  
  44. ld sum_recursive_range(const vld &v, int l, int r)
  45. {
  46. if (l == r)
  47. return v[r];
  48.  
  49. int mid = (l + r) / 2;
  50. return sum_recursive_range(v, l, mid) + sum_recursive_range(v, mid + 1, r);
  51. }
  52.  
  53. ld sum_recursive(vld v)
  54. {
  55. random_shuffle(v.begin(), v.end());
  56. return sum_recursive_range(v, 0, (int)v.size() - 1);
  57. }
  58.  
  59. ld sum_huffman(vld v)
  60. {
  61. priority_queue<ld, vector<ld>, greater<ld>> q(v.begin(), v.end());
  62. while (q.size() != 1)
  63. {
  64. auto d1 = q.top();
  65. q.pop();
  66. auto d2 = q.top();
  67. q.pop();
  68. q.push(d1 + d2);
  69. }
  70. return q.top();
  71. }
  72.  
  73. void dump(string prefix, ld x, ld sum)
  74. {
  75. cout << prefix << ": " << x << " (error " << fabs(x - sum) << ")" << endl;
  76. }
  77.  
  78. void dump(int n)
  79. {
  80. auto input = generate(n);
  81. auto v = input.first;
  82.  
  83. sort(v.begin(), v.end());
  84.  
  85. cout << "Case n = " << n << endl;
  86. cout << "Series sum: " << input.second << endl;
  87.  
  88. dump("Naive direct", sum_naive(v), input.second);
  89. reverse(v.begin(), v.end());
  90. dump("Naive reversed", sum_naive(v), input.second);
  91.  
  92. dump("Recursive 1", sum_recursive(v), input.second);
  93. dump("Recursive 2", sum_recursive(v), input.second);
  94. dump("Recursive 3", sum_recursive(v), input.second);
  95. dump("Recursive 4", sum_recursive(v), input.second);
  96.  
  97. dump("Huffman", sum_huffman(v), input.second);
  98. }
  99.  
  100. int main()
  101. {
  102. srand(322);
  103.  
  104. cout.setf(ios::fixed);
  105. cout.precision(20);
  106.  
  107. dump(100000);
  108. dump(100000);
  109. dump(1000000);
  110. dump(1000000);
  111.  
  112. return 0;
  113. }
  114.  
  115.  
Success #stdin #stdout 0.78s 16040KB
stdin
Standard input is empty
stdout
Case n = 100000
Series sum: 609544036.50378978252410888672
Naive direct: 609544036.50378096103668212891 (error 0.00000882148742675781)
Naive reversed: 609544036.50378334522247314453 (error 0.00000643730163574219)
Recursive 1: 609544036.50378966331481933594 (error 0.00000011920928955078)
Recursive 2: 609544036.50378966331481933594 (error 0.00000011920928955078)
Recursive 3: 609544036.50378966331481933594 (error 0.00000011920928955078)
Recursive 4: 609544036.50378966331481933594 (error 0.00000011920928955078)
Huffman: 609544036.50378978252410888672 (error 0.00000000000000000000)
Case n = 100000
Series sum: 609509717.47435688972473144531
Naive direct: 609509717.47435772418975830078 (error 0.00000083446502685547)
Naive reversed: 609509717.47436237335205078125 (error 0.00000548362731933594)
Recursive 1: 609509717.47435700893402099609 (error 0.00000011920928955078)
Recursive 2: 609509717.47435700893402099609 (error 0.00000011920928955078)
Recursive 3: 609509717.47435688972473144531 (error 0.00000000000000000000)
Recursive 4: 609509717.47435688972473144531 (error 0.00000000000000000000)
Huffman: 609509717.47435688972473144531 (error 0.00000000000000000000)
Case n = 1000000
Series sum: 6090154272.06175231933593750000
Naive direct: 6090154272.06171894073486328125 (error 0.00003337860107421875)
Naive reversed: 6090154272.06146812438964843750 (error 0.00028419494628906250)
Recursive 1: 6090154272.06175231933593750000 (error 0.00000000000000000000)
Recursive 2: 6090154272.06175231933593750000 (error 0.00000000000000000000)
Recursive 3: 6090154272.06175231933593750000 (error 0.00000000000000000000)
Recursive 4: 6090154272.06175231933593750000 (error 0.00000000000000000000)
Huffman: 6090154272.06175231933593750000 (error 0.00000000000000000000)
Case n = 1000000
Series sum: 6092192544.64155769348144531250
Naive direct: 6092192544.64142417907714843750 (error 0.00013351440429687500)
Naive reversed: 6092192544.64191722869873046875 (error 0.00035953521728515625)
Recursive 1: 6092192544.64155769348144531250 (error 0.00000000000000000000)
Recursive 2: 6092192544.64155769348144531250 (error 0.00000000000000000000)
Recursive 3: 6092192544.64155769348144531250 (error 0.00000000000000000000)
Recursive 4: 6092192544.64155769348144531250 (error 0.00000000000000000000)
Huffman: 6092192544.64155769348144531250 (error 0.00000000000000000000)