fork(1) 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() % 10);
  24.  
  25. vld result;
  26. int 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.58s 54640KB
stdin
Standard input is empty
stdout
Case n = 100000
Series sum: 549947.52269495825748890638
Naive direct: 549947.52269534918013960123 (error 0.00000039092265069485)
Naive reversed: 549947.52269447059370577335 (error 0.00000048766378313303)
Recursive 1: 549947.52269495825748890638 (error 0.00000000000000000000)
Recursive 2: 549947.52269495825748890638 (error 0.00000000000000000000)
Recursive 3: 549947.52269495837390422821 (error 0.00000000011641532183)
Recursive 4: 549947.52269495825748890638 (error 0.00000000000000000000)
Huffman: 549947.52269495825748890638 (error 0.00000000000000000000)
Case n = 100000
Series sum: 547937.35590177006088197231
Naive direct: 547937.35590214678086340427 (error 0.00000037671998143196)
Naive reversed: 547937.35590132442303001881 (error 0.00000044563785195351)
Recursive 1: 547937.35590177006088197231 (error 0.00000000000000000000)
Recursive 2: 547937.35590177006088197231 (error 0.00000000000000000000)
Recursive 3: 547937.35590177006088197231 (error 0.00000000000000000000)
Recursive 4: 547937.35590177006088197231 (error 0.00000000000000000000)
Huffman: 547937.35590177006088197231 (error 0.00000000000000000000)
Case n = 1000000
Series sum: 5476400.94872959144413471222
Naive direct: 5476400.94871313124895095825 (error 0.00001646019518375397)
Naive reversed: 5476400.94877797737717628479 (error 0.00004838593304157257)
Recursive 1: 5476400.94872959144413471222 (error 0.00000000000000000000)
Recursive 2: 5476400.94872959237545728683 (error 0.00000000093132257462)
Recursive 3: 5476400.94872959144413471222 (error 0.00000000000000000000)
Recursive 4: 5476400.94872959144413471222 (error 0.00000000000000000000)
Huffman: 5476400.94872959144413471222 (error 0.00000000000000000000)
Case n = 1000000
Series sum: 5477473.03768595866858959198
Naive direct: 5477473.03766947053372859955 (error 0.00001648813486099243)
Naive reversed: 5477473.03773453924804925919 (error 0.00004858057945966721)
Recursive 1: 5477473.03768595959991216660 (error 0.00000000093132257462)
Recursive 2: 5477473.03768595866858959198 (error 0.00000000000000000000)
Recursive 3: 5477473.03768595866858959198 (error 0.00000000000000000000)
Recursive 4: 5477473.03768595959991216660 (error 0.00000000093132257462)
Huffman: 5477473.03768595866858959198 (error 0.00000000000000000000)