Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4.  
  5. struct FootballPlayer {
  6.     int64_t efficiency;
  7.     size_t index;
  8. };
  9.  
  10. template<typename Iter, typename Comporator>
  11. void MergeSort(Iter begin_iter, Iter end_iter, Comporator comp) {
  12.     if (end_iter - begin_iter <= 1) {
  13.         return;
  14.     }
  15.     Iter mid_iter = begin_iter + (end_iter - begin_iter) / 2;
  16.     MergeSort(begin_iter, mid_iter, comp);
  17.     MergeSort(mid_iter, end_iter, comp);
  18.     Iter left_iter  = begin_iter;
  19.     Iter right_iter = mid_iter;
  20.     std::vector<FootballPlayer> merged_array;
  21.     while (left_iter != mid_iter && right_iter != end_iter) {
  22.         if (comp(*left_iter, *right_iter)) {
  23.             merged_array.push_back(*left_iter);
  24.             ++left_iter;
  25.         } else {
  26.             merged_array.push_back(*right_iter);
  27.             ++right_iter;
  28.         }
  29.     }
  30.     while (left_iter != mid_iter) {
  31.         merged_array.push_back(*left_iter);
  32.         ++left_iter;
  33.     }
  34.     while (right_iter != end_iter) {
  35.         merged_array.push_back(*right_iter);
  36.         ++right_iter;
  37.     }
  38.     Iter cur_iter = begin_iter;
  39.     size_t pos = 0;
  40.     while (cur_iter != end_iter) {
  41.         *cur_iter = merged_array[pos];
  42.         ++cur_iter;
  43.         ++pos;
  44.     }
  45. }
  46.  
  47. bool CompareByEffeciency(const FootballPlayer& first,
  48. const FootballPlayer& second) {
  49.     return first.efficiency < second.efficiency;
  50. }
  51.  
  52. bool CompareByIndex(const FootballPlayer& first,
  53. const FootballPlayer& second) {
  54.     return first.index < second.index;
  55. }
  56.  
  57. int64_t BuildMostEffectiveSolidaryTeam(
  58.     std::vector<FootballPlayer>& footbal_team,
  59.     std::vector<size_t>& indices_of_best_team) {
  60.     if (footbal_team.size() == 1) {
  61.         indices_of_best_team.push_back(footbal_team.back().index);
  62.         return footbal_team.back().efficiency;
  63.     }
  64.     MergeSort(footbal_team.begin(), footbal_team.end(), CompareByEffeciency);
  65.     size_t right_index = 1;
  66.     int64_t current_sum = footbal_team[0].efficiency;
  67.     int64_t ans = 0;
  68.     size_t left_position = 0;
  69.     size_t right_position = 0;
  70.     for (size_t ind = 0; ind + 1 < footbal_team.size(); ++ind) {
  71.         while (right_index < footbal_team.size() &&
  72.         footbal_team[right_index].efficiency <= footbal_team[ind].efficiency +
  73.         footbal_team[ind + 1].efficiency) {
  74.             current_sum += footbal_team[right_index].efficiency;
  75.             ++right_index;
  76.         }
  77.         if (current_sum > ans) {
  78.             ans = current_sum;
  79.             left_position = ind;
  80.             right_position = right_index - 1;
  81.         }
  82.         current_sum -= footbal_team[ind].efficiency;
  83.     }
  84.     std::vector<FootballPlayer> players_of_best_team;
  85.     players_of_best_team.reserve(right_position + 1 - left_position);
  86.     for (size_t i = left_position; i <= right_position; ++i) {
  87.         players_of_best_team.push_back(footbal_team[i]);
  88.     }
  89.     MergeSort(players_of_best_team.begin(), players_of_best_team.end(),
  90.     CompareByIndex);
  91.     for (auto player : players_of_best_team) {
  92.         indices_of_best_team.push_back(player.index);
  93.     }
  94.     return ans;
  95. }
  96.  
  97.  
  98. int main() {
  99.     std::ios_base::sync_with_stdio(0);
  100.     std::cin.tie(nullptr);
  101.     size_t numb_of_players;
  102.     std::cin >> numb_of_players;
  103.     std::vector<FootballPlayer> football_team;
  104.     football_team.reserve(numb_of_players);
  105.     for (size_t i = 1; i <= numb_of_players; ++i) {
  106.         int64_t value;
  107.         std::cin >> value;
  108.         FootballPlayer cur;
  109.         cur.efficiency = value;
  110.         cur.index = i;
  111.         football_team.push_back(cur);
  112.     }
  113.     std::vector<size_t> indices_of_best_team;
  114.     std::cout << BuildMostEffectiveSolidaryTeam(football_team, indices_of_best_team);
  115.     std::cout << std::endl;
  116.     for (auto ind : indices_of_best_team) {
  117.         std::cout << ind << " ";
  118.     }
  119.     std::cout << std::endl;
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement