vadimk772336

работает, очищен

Dec 21st, 2021 (edited)
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. struct group_info
  6. {
  7.     int group_size;
  8.     int first_element;
  9. };
  10.  
  11. int main()
  12. {
  13.     int N, M, K;
  14.     std::cin >> N >> M >> K;
  15.  
  16.     std::vector<struct group_info> groups;
  17.     std::vector<int> partial_sums(N + 1, 0);
  18.     std::vector<int> partial_cost_sums(N + 1, 0);
  19.     std::vector<int> stages(N);
  20.  
  21.  
  22.     int s1 = 0;
  23.     int s2 = 0;
  24.     for (int i = 0; i < N; ++i)
  25.     {
  26.         std::cin >> stages[i];
  27.         s1 += stages[i];
  28.         s2 += ceil((double)stages[i] / K);
  29.         partial_sums[i + 1] = s1;
  30.         partial_cost_sums[i + 1] = s2;
  31.     }
  32.  
  33.     int i = N - 1;
  34.     int left, right;
  35.     int sum;
  36.  
  37.     int local_max, local_max_idx;
  38.     int cost_joined, cost_separated;
  39.     int profit = 0;
  40.     int count_groups = 0;
  41.  
  42.     struct group_info group;
  43.     bool flag;
  44.     while (i > 0)
  45.     {
  46.         if (stages[i] % M == 0)
  47.             i--;
  48.  
  49.         else
  50.         {
  51.             left = (i - M + 1) * ((i - M + 1) > 0);
  52.             local_max_idx = left;
  53.             local_max = 0;
  54.             right = i + 1;
  55.             sum = partial_sums[right] - partial_sums[left];
  56.             flag = true;
  57.  
  58.             while (flag & right - left > 1)
  59.             {
  60.                 if (sum % M == 0)
  61.                     flag = false;
  62.  
  63.                 else
  64.                 {
  65.                     cost_joined = ceil((double)sum / K);
  66.                     cost_separated = partial_cost_sums[right] - partial_cost_sums[left];
  67.                     if (local_max <= cost_separated - cost_joined)
  68.                     {
  69.                         local_max = cost_separated - cost_joined;
  70.                         local_max_idx = left;
  71.                     }
  72.                     sum -= stages[left];
  73.                     left++;
  74.                 }
  75.             }
  76.  
  77.             if (sum % M != 0 & local_max > 0)
  78.             {
  79.                 group.group_size = right - local_max_idx;
  80.                 group.first_element = local_max_idx + 1;
  81.                 groups.push_back(group);
  82.                 profit += local_max;
  83.                 i -= group.group_size;
  84.                 count_groups++;
  85.             }
  86.  
  87.             else if (sum % M == 0)
  88.             {
  89.                 cost_joined = sum / K;
  90.                 cost_separated = partial_cost_sums[right] - partial_cost_sums[left];
  91.                 profit += cost_separated - cost_joined;
  92.                 group.group_size = right - left;
  93.                 group.first_element = left + 1;
  94.                 groups.push_back(group);
  95.                 i -= group.group_size;
  96.                 count_groups++;
  97.             }
  98.  
  99.             else
  100.                 --i;
  101.         }
  102.     }
  103.  
  104.  
  105.     std::cout << profit << std::endl;
  106.     std::cout << count_groups << std::endl;
  107.     for (int i = count_groups - 1; i > -1; --i)
  108.         std::cout << groups[i].first_element << " " << groups[i].group_size << std::endl;
  109.  
  110.     return 0;
  111. }
  112.  
Add Comment
Please, Sign In to add comment