Advertisement
AshTurner67

DSA Offline 7

Nov 22nd, 2024 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <tuple>
  4.  
  5. using namespace std;
  6.  
  7. tuple<int, int, int> calculateStreak(vector<int> &scores, int start, int end)
  8. {
  9.     if (end == start)
  10.         return {start, end, scores[start]};
  11.     int mid = (start + end) / 2;
  12.     tuple<int, int, int> left = calculateStreak(scores, start, mid);
  13.     tuple<int, int, int> right = calculateStreak(scores, mid + 1, end);
  14.     int crossingStart = mid, crossingEnd = mid + 1;
  15.     int leftSum = 0, rightSum = 0;
  16.     int maxLeft, maxRight, i;
  17.     bool flag = false;
  18.     maxLeft = maxRight = INT_MIN;
  19.     for (i = crossingStart; i >= start; i--)
  20.     {
  21.         leftSum += scores[i];
  22.         if (leftSum > maxLeft)
  23.             maxLeft = leftSum, crossingStart = i;
  24.     }
  25.     for (i = crossingEnd; i <= end; i++)
  26.     {
  27.         if (rightSum < 0)
  28.             break;
  29.         rightSum += scores[i];
  30.         if (rightSum > maxRight)
  31.             maxRight = rightSum, crossingEnd = i;
  32.     }
  33.     int crossingSum = maxLeft + maxRight;
  34.     tuple<int, int, int> crossingStreak = {crossingStart, crossingEnd, crossingSum};
  35.     tuple<int, int, int> ret;
  36.     if (get<2>(left) > get<2>(right))
  37.         ret = left;
  38.     else
  39.     {
  40.         if (get<2>(left) == get<2>(right))
  41.         {
  42.             if ((get<1>(left) - get<0>(left)) < (get<1>(right) - get<0>(right)))
  43.                 ret = left;
  44.             else
  45.                 ret = right;
  46.         }
  47.         else
  48.             ret = right;
  49.     }
  50.     if (get<2>(ret) > get<2>(crossingStreak))
  51.         ret = ret;
  52.     else
  53.     {
  54.         if (get<2>(ret) == get<2>(crossingStreak))
  55.         {
  56.             if ((get<1>(ret) - get<0>(ret)) < (get<1>(crossingStreak) - get<0>(crossingStreak)))
  57.                 ret = ret;
  58.             else
  59.                 ret = crossingStreak;
  60.         }
  61.         else
  62.             ret = crossingStreak;
  63.     }
  64.     return ret;
  65. }
  66.  
  67. void printStreak(vector<int> &scores, int n)
  68. {
  69.     int start = 0, end = n - 1;
  70.     tuple<int, int, int> result = {start, end, scores[0]};
  71.     if (n > 1)
  72.         result = calculateStreak(scores, start, end);
  73.     if (get<2>(result) > 0)
  74.     {
  75.         cout << "[";
  76.         for (int i = get<0>(result); i < get<1>(result); i++)
  77.             cout << scores[i] << ", ";
  78.         cout << scores[get<1>(result)] << "] with a sum of " << get<2>(result);
  79.     }
  80.     else
  81.         cout << "[] with a sum 0";
  82. }
  83.  
  84. int main()
  85. {
  86.  
  87.     int n;
  88.     cin >> n;
  89.     vector<int> scores;
  90.     for (int i = 0; i < n; i++)
  91.     {
  92.         int temp;
  93.         cin >> temp;
  94.         scores.push_back(temp);
  95.     }
  96.     printStreak(scores, n);
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement