Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using std::cin;
- using std::cout;
- using std::swap;
- using std::vector;
- struct player {
- int64_t value;
- int index;
- };
- bool CompareIndex(player one, player two) {
- return one.index < two.index;
- }
- void heapify(vector<player> &arr, int size, int index, int type)
- {
- int largest = index;
- int left = 2 * index + 1;
- int right = 2 * index + 2;
- if (type == 0) {
- if (left < size && arr[left].value > arr[largest].value) {
- largest = left;
- }
- if (right < size && arr[right].value > arr[largest].value) {
- largest = right;
- }
- }
- else {
- if (left < size && arr[left].index > arr[largest].index) {
- largest = left;
- }
- if (right < size && arr[right].index > arr[largest].index) {
- largest = right;
- }
- }
- if (largest != index)
- {
- std::swap(arr[index], arr[largest]);
- heapify(arr, size, largest, type);
- }
- }
- void heapSort(vector<player> &arr, int size, int type)
- {
- for (int index = size / 2 - 1; index >= 0; --index) {
- heapify(arr, size, index, type);
- }
- for (int index = size - 1; index >= 0; --index)
- {
- std::swap(arr[0], arr[index]);
- heapify(arr, index, 0, type);
- }
- }
- vector<int> getBestCommand(vector<player> times) {
- vector<int>pointer(2);
- int count = times.size();
- int i_weak = 0;
- int i_strong = 1;
- int true_left = 0;
- int true_rigth = 1;
- int64_t result_sum = 0;
- int64_t sum = times[i_weak].value;
- int64_t equal_two = times[i_weak].value + times[i_weak + 1].value;
- while (i_strong < count) {
- if (times[i_strong].value > equal_two) {
- sum -= times[i_weak].value;
- i_weak++;
- equal_two = times[i_weak].value + times[i_weak + 1].value;
- }
- else if (times[i_strong].value <= equal_two) {
- sum += times[i_strong].value;
- i_strong++;
- if (sum > result_sum) {
- result_sum = sum;
- true_left = i_weak;
- true_rigth = i_strong;
- }
- }
- }
- pointer[0] = true_left;
- pointer[1] = true_rigth;
- cout << result_sum << "\n";
- return pointer;
- }
- void run(vector<player> ×) {
- int count = times.size();
- if (count == 1) {
- cout << times[0].value << "\n" << times[0].index;
- }
- else if (count == 2) {
- cout << times[0].value + times[1].value << "\n";
- if (times[0].index > times[1].index) {
- cout << times[1].index << " " << times[0].index;
- }
- else {
- cout << times[0].index << " " << times[1].index;
- }
- }
- else {
- heapSort(times, times.size(), 0);
- vector<int>pointer = getBestCommand(times);
- std::sort(times.begin() + pointer[0], times.begin() + pointer[1], CompareIndex);
- for (; pointer[0] < pointer[1]; ++pointer[0]) {
- cout << times[pointer[0]].index << " ";
- }
- }
- }
- int main() {
- std::ios_base::sync_with_stdio(0);
- int count;
- cin >> count;
- vector <player> times(count);
- for (int index = 0; index < count; ++index) {
- cin >> times[index].value;
- times[index].index = index + 1;
- }
- run(times);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement