Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <tuple>
- using namespace std;
- tuple<int, int, int> calculateStreak(vector<int> &scores, int start, int end)
- {
- if (end == start)
- return {start, end, scores[start]};
- int mid = (start + end) / 2;
- tuple<int, int, int> left = calculateStreak(scores, start, mid);
- tuple<int, int, int> right = calculateStreak(scores, mid + 1, end);
- int crossingStart = mid, crossingEnd = mid + 1;
- int leftSum = 0, rightSum = 0;
- int maxLeft, maxRight, i;
- bool flag = false;
- maxLeft = maxRight = INT_MIN;
- for (i = crossingStart; i >= start; i--)
- {
- leftSum += scores[i];
- if (leftSum > maxLeft)
- maxLeft = leftSum, crossingStart = i;
- }
- for (i = crossingEnd; i <= end; i++)
- {
- if (rightSum < 0)
- break;
- rightSum += scores[i];
- if (rightSum > maxRight)
- maxRight = rightSum, crossingEnd = i;
- }
- int crossingSum = maxLeft + maxRight;
- tuple<int, int, int> crossingStreak = {crossingStart, crossingEnd, crossingSum};
- tuple<int, int, int> ret;
- if (get<2>(left) > get<2>(right))
- ret = left;
- else
- {
- if (get<2>(left) == get<2>(right))
- {
- if ((get<1>(left) - get<0>(left)) < (get<1>(right) - get<0>(right)))
- ret = left;
- else
- ret = right;
- }
- else
- ret = right;
- }
- if (get<2>(ret) > get<2>(crossingStreak))
- ret = ret;
- else
- {
- if (get<2>(ret) == get<2>(crossingStreak))
- {
- if ((get<1>(ret) - get<0>(ret)) < (get<1>(crossingStreak) - get<0>(crossingStreak)))
- ret = ret;
- else
- ret = crossingStreak;
- }
- else
- ret = crossingStreak;
- }
- return ret;
- }
- void printStreak(vector<int> &scores, int n)
- {
- int start = 0, end = n - 1;
- tuple<int, int, int> result = {start, end, scores[0]};
- if (n > 1)
- result = calculateStreak(scores, start, end);
- if (get<2>(result) > 0)
- {
- cout << "[";
- for (int i = get<0>(result); i < get<1>(result); i++)
- cout << scores[i] << ", ";
- cout << scores[get<1>(result)] << "] with a sum of " << get<2>(result);
- }
- else
- cout << "[] with a sum 0";
- }
- int main()
- {
- int n;
- cin >> n;
- vector<int> scores;
- for (int i = 0; i < n; i++)
- {
- int temp;
- cin >> temp;
- scores.push_back(temp);
- }
- printStreak(scores, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement