Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using std::cin;
  6. using std::cout;
  7. using std::swap;
  8. using std::vector;
  9.  
  10. struct player {
  11.   int64_t value;
  12.   int index;
  13. };
  14.  
  15. bool CompareIndex(player one, player two) {
  16.   return one.index < two.index;
  17. }
  18.  
  19. void heapify(vector<player> &arr, int size, int index, int type)
  20. {
  21.   int largest = index;
  22.   int left = 2 * index + 1;
  23.   int right = 2 * index + 2;
  24.  
  25.   if (type == 0) {
  26.     if (left < size && arr[left].value > arr[largest].value) {
  27.       largest = left;
  28.     }
  29.  
  30.     if (right < size && arr[right].value > arr[largest].value) {
  31.       largest = right;
  32.     }
  33.   }
  34.   else {
  35.     if (left < size && arr[left].index > arr[largest].index) {
  36.       largest = left;
  37.     }
  38.  
  39.     if (right < size && arr[right].index > arr[largest].index) {
  40.       largest = right;
  41.     }
  42.   }
  43.  
  44.   if (largest != index)
  45.   {
  46.     std::swap(arr[index], arr[largest]);
  47.     heapify(arr, size, largest, type);
  48.   }
  49. }
  50.  
  51. void heapSort(vector<player> &arr, int size, int type)
  52. {
  53.   for (int index = size / 2 - 1; index >= 0; --index) {
  54.     heapify(arr, size, index, type);
  55.   }
  56.  
  57.   for (int index = size - 1; index >= 0; --index)
  58.   {
  59.     std::swap(arr[0], arr[index]);
  60.     heapify(arr, index, 0, type);
  61.   }
  62. }
  63.  
  64. vector<int> getBestCommand(vector<player> times) {
  65.   vector<int>pointer(2);
  66.   int count = times.size();
  67.   int i_weak = 0;
  68.   int i_strong = 1;
  69.   int true_left = 0;
  70.   int true_rigth = 1;
  71.   int64_t result_sum = 0;
  72.   int64_t sum = times[i_weak].value;
  73.   int64_t equal_two = times[i_weak].value + times[i_weak + 1].value;
  74.  
  75.   while (i_strong < count) {
  76.     if (times[i_strong].value > equal_two) {
  77.       sum -= times[i_weak].value;
  78.       i_weak++;
  79.       equal_two = times[i_weak].value + times[i_weak + 1].value;
  80.     }
  81.     else if (times[i_strong].value <= equal_two) {
  82.       sum += times[i_strong].value;
  83.       i_strong++;
  84.       if (sum > result_sum) {
  85.         result_sum = sum;
  86.         true_left = i_weak;
  87.         true_rigth = i_strong;
  88.       }
  89.     }
  90.   }
  91.  
  92.   pointer[0] = true_left;
  93.   pointer[1] = true_rigth;
  94.  
  95.   cout << result_sum << "\n";
  96.  
  97.   return pointer;
  98. }
  99.  
  100. void run(vector<player> &times) {
  101.   int count = times.size();
  102.  
  103.   if (count == 1) {
  104.     cout << times[0].value << "\n" << times[0].index;
  105.   }
  106.   else if (count == 2) {
  107.     cout << times[0].value + times[1].value << "\n";
  108.     if (times[0].index > times[1].index) {
  109.       cout << times[1].index << " " << times[0].index;
  110.     }
  111.     else {
  112.       cout << times[0].index << " " << times[1].index;
  113.     }
  114.   }
  115.   else {
  116.     heapSort(times, times.size(), 0);
  117.  
  118.     vector<int>pointer = getBestCommand(times);
  119.  
  120.     std::sort(times.begin() + pointer[0], times.begin() + pointer[1], CompareIndex);
  121.  
  122.     for (; pointer[0] < pointer[1]; ++pointer[0]) {
  123.       cout << times[pointer[0]].index << " ";
  124.     }
  125.   }
  126. }
  127.  
  128. int main() {
  129.   std::ios_base::sync_with_stdio(0);
  130.   int count;
  131.   cin >> count;
  132.   vector <player> times(count);
  133.  
  134.   for (int index = 0; index < count; ++index) {
  135.     cin >> times[index].value;
  136.     times[index].index = index + 1;
  137.   }
  138.  
  139.   run(times);
  140.  
  141.   return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement