Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- struct FootballPlayer {
- int64_t efficiency;
- size_t index;
- };
- template<typename Iter, typename Comporator>
- void MergeSort(Iter begin_iter, Iter end_iter, Comporator comp) {
- if (end_iter - begin_iter <= 1) {
- return;
- }
- Iter mid_iter = begin_iter + (end_iter - begin_iter) / 2;
- MergeSort(begin_iter, mid_iter, comp);
- MergeSort(mid_iter, end_iter, comp);
- Iter left_iter = begin_iter;
- Iter right_iter = mid_iter;
- std::vector<FootballPlayer> merged_array;
- while (left_iter != mid_iter && right_iter != end_iter) {
- if (comp(*left_iter, *right_iter)) {
- merged_array.push_back(*left_iter);
- ++left_iter;
- } else {
- merged_array.push_back(*right_iter);
- ++right_iter;
- }
- }
- while (left_iter != mid_iter) {
- merged_array.push_back(*left_iter);
- ++left_iter;
- }
- while (right_iter != end_iter) {
- merged_array.push_back(*right_iter);
- ++right_iter;
- }
- Iter cur_iter = begin_iter;
- size_t pos = 0;
- while (cur_iter != end_iter) {
- *cur_iter = merged_array[pos];
- ++cur_iter;
- ++pos;
- }
- }
- bool CompareByEffeciency(const FootballPlayer& first,
- const FootballPlayer& second) {
- return first.efficiency < second.efficiency;
- }
- bool CompareByIndex(const FootballPlayer& first,
- const FootballPlayer& second) {
- return first.index < second.index;
- }
- int64_t BuildMostEffectiveSolidaryTeam(
- std::vector<FootballPlayer>& footbal_team,
- std::vector<size_t>& indices_of_best_team) {
- if (footbal_team.size() == 1) {
- indices_of_best_team.push_back(footbal_team.back().index);
- return footbal_team.back().efficiency;
- }
- MergeSort(footbal_team.begin(), footbal_team.end(), CompareByEffeciency);
- size_t right_index = 1;
- int64_t current_sum = footbal_team[0].efficiency;
- int64_t ans = 0;
- size_t left_position = 0;
- size_t right_position = 0;
- for (size_t ind = 0; ind + 1 < footbal_team.size(); ++ind) {
- while (right_index < footbal_team.size() &&
- footbal_team[right_index].efficiency <= footbal_team[ind].efficiency +
- footbal_team[ind + 1].efficiency) {
- current_sum += footbal_team[right_index].efficiency;
- ++right_index;
- }
- if (current_sum > ans) {
- ans = current_sum;
- left_position = ind;
- right_position = right_index - 1;
- }
- current_sum -= footbal_team[ind].efficiency;
- }
- std::vector<FootballPlayer> players_of_best_team;
- players_of_best_team.reserve(right_position + 1 - left_position);
- for (size_t i = left_position; i <= right_position; ++i) {
- players_of_best_team.push_back(footbal_team[i]);
- }
- MergeSort(players_of_best_team.begin(), players_of_best_team.end(),
- CompareByIndex);
- for (auto player : players_of_best_team) {
- indices_of_best_team.push_back(player.index);
- }
- return ans;
- }
- int main() {
- std::ios_base::sync_with_stdio(0);
- std::cin.tie(nullptr);
- size_t numb_of_players;
- std::cin >> numb_of_players;
- std::vector<FootballPlayer> football_team;
- football_team.reserve(numb_of_players);
- for (size_t i = 1; i <= numb_of_players; ++i) {
- int64_t value;
- std::cin >> value;
- FootballPlayer cur;
- cur.efficiency = value;
- cur.index = i;
- football_team.push_back(cur);
- }
- std::vector<size_t> indices_of_best_team;
- std::cout << BuildMostEffectiveSolidaryTeam(football_team, indices_of_best_team);
- std::cout << std::endl;
- for (auto ind : indices_of_best_team) {
- std::cout << ind << " ";
- }
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement