Advertisement
mfgnik

Untitled

Jul 13th, 2020
1,510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <deque>
  3. #include <vector>
  4. #include <algorithm>
  5. int main() {
  6.     int columns_amount, segment_len;
  7.     std::cin >> columns_amount >> segment_len;
  8.     std::vector<int> num_list(columns_amount);
  9.     int64_t segment_sum = 0;
  10.     for (int index = 0; index < columns_amount; ++index) {
  11.         std::cin >> num_list[index];
  12.     }
  13.     std::deque<int> my_deque;
  14.     std::vector<std::pair<int64_t, std::vector<int>>> bl(segment_len + 2);
  15.     for (int index = 0; index < columns_amount; ++index) {
  16.         segment_sum += num_list[index];
  17.         if (index >= segment_len) {
  18.             segment_sum -= num_list[index - segment_len];
  19.             if (my_deque.front() == index - segment_len) {
  20.                 my_deque.pop_front();
  21.             }
  22.         }
  23.         while (!my_deque.empty() && num_list[my_deque.back()] >= num_list[index]) {
  24.             my_deque.pop_back();
  25.         }
  26.         my_deque.push_back(index);
  27.         if (index >= segment_len - 1) {
  28.             auto index_prev = (index - 1) % (segment_len + 1);
  29.             auto index_k_prev = (index - segment_len) % (segment_len + 1);
  30.             auto index_current = index % (segment_len + 1);
  31.             int64_t protection = num_list[my_deque.front()] * segment_sum;
  32.             std::pair<int64_t, std::vector<int>> another = {bl[index_k_prev].first + protection, bl[index_k_prev].second};
  33.             another.second.push_back(index - segment_len + 2);
  34.             bl[index_current] = std::max(bl[index_prev], another);
  35.         }
  36.     }
  37.     auto max_elem = bl.begin();
  38.     for (auto it = bl.begin(); it != bl.end(); ++it) {
  39.         if (it->first > max_elem->first) {
  40.             max_elem = it;
  41.         }
  42.     }
  43.     std::cout << max_elem->second.size() << "\n";
  44.     for (auto element : max_elem->second) {
  45.         std::cout << element << " ";
  46.     }
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement