fork(4) download
  1. #include <climits>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct T {
  7. int mn, mx, diff;
  8. // conquer
  9. T operator+(T const &rhs) const {
  10. int _mn = min(mn, rhs.mn);
  11. int _mx = max(mx, rhs.mx);
  12. int _diff = max({diff, rhs.diff, rhs.mx-mn});
  13. return {_mn, _mx, _diff};
  14. }
  15. };
  16.  
  17. // max diff is assumed 0 if a is mono decreasing
  18. T max_diff(int a[], int st, int ed) {
  19. if (st >= ed) return {INT_MAX, INT_MIN, 0};
  20. if (st + 1 == ed) return {a[st], a[st], 0};
  21. // divide
  22. int md = st + (ed - st) / 2;
  23. return max_diff(a, st, md) + max_diff(a, md, ed);
  24. }
  25.  
  26. int main() {
  27. int a[] = {12, 9, 18, 3, 7, 11, 6, 15, 6, 1, 10};
  28. cout << max_diff(a, 0, sizeof(a)/sizeof(int)).diff << endl;
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
12