Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- struct group_info
- {
- int group_size;
- int first_element;
- };
- int main()
- {
- int N, M, K;
- std::cin >> N >> M >> K;
- std::vector<struct group_info> groups;
- std::vector<int> partial_sums(N + 1, 0);
- std::vector<int> partial_cost_sums(N + 1, 0);
- std::vector<int> stages(N);
- int s1 = 0;
- int s2 = 0;
- for (int i = 0; i < N; ++i)
- {
- std::cin >> stages[i];
- s1 += stages[i];
- s2 += ceil((double)stages[i] / K);
- partial_sums[i + 1] = s1;
- partial_cost_sums[i + 1] = s2;
- }
- int i = N - 1;
- int left, right;
- int sum;
- int local_max, local_max_idx;
- int cost_joined, cost_separated;
- int profit = 0;
- int count_groups = 0;
- struct group_info group;
- bool flag;
- while (i > 0)
- {
- if (stages[i] % M == 0)
- i--;
- else
- {
- left = (i - M + 1) * ((i - M + 1) > 0);
- local_max_idx = left;
- local_max = 0;
- right = i + 1;
- sum = partial_sums[right] - partial_sums[left];
- flag = true;
- while (flag & right - left > 1)
- {
- if (sum % M == 0)
- flag = false;
- else
- {
- cost_joined = ceil((double)sum / K);
- cost_separated = partial_cost_sums[right] - partial_cost_sums[left];
- if (local_max <= cost_separated - cost_joined)
- {
- local_max = cost_separated - cost_joined;
- local_max_idx = left;
- }
- sum -= stages[left];
- left++;
- }
- }
- if (sum % M != 0 & local_max > 0)
- {
- group.group_size = right - local_max_idx;
- group.first_element = local_max_idx + 1;
- groups.push_back(group);
- profit += local_max;
- i -= group.group_size;
- count_groups++;
- }
- else if (sum % M == 0)
- {
- cost_joined = sum / K;
- cost_separated = partial_cost_sums[right] - partial_cost_sums[left];
- profit += cost_separated - cost_joined;
- group.group_size = right - left;
- group.first_element = left + 1;
- groups.push_back(group);
- i -= group.group_size;
- count_groups++;
- }
- else
- --i;
- }
- }
- std::cout << profit << std::endl;
- std::cout << count_groups << std::endl;
- for (int i = count_groups - 1; i > -1; --i)
- std::cout << groups[i].first_element << " " << groups[i].group_size << std::endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment