Advertisement
Josif_tepe

Untitled

Feb 5th, 2023
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <cmath>
  5. #include <set>
  6. #include <array>
  7. using namespace std;
  8. const int maxn = 2e5 + 10;
  9. typedef long long ll;
  10. multiset<int> smaller_values, greater_values;
  11. int k;
  12. void insert_value(int x, ll & difference_smaller, ll & sum_greater) {
  13.     int max_in_smaller = *smaller_values.rbegin();
  14.    
  15.     if(max_in_smaller < x) {
  16.         greater_values.insert(x);
  17.         sum_greater += x;
  18.         if(greater_values.size() > k / 2) {
  19.             int min_in_greater = *greater_values.begin();
  20.             smaller_values.insert(min_in_greater);
  21.             greater_values.erase(greater_values.find(min_in_greater));
  22.            
  23.             sum_greater -= min_in_greater;
  24.             difference_smaller -= min_in_greater;
  25.         }
  26.     }
  27.     else {
  28.         smaller_values.insert(x);
  29.         difference_smaller -= x;
  30.         if(smaller_values.size() > (k + 1) / 2) {
  31.             int tmp_max_in_smaller = *smaller_values.rbegin();
  32.             greater_values.insert(tmp_max_in_smaller);
  33.             smaller_values.erase(smaller_values.find(tmp_max_in_smaller));
  34.            
  35.             difference_smaller += tmp_max_in_smaller;
  36.             sum_greater += tmp_max_in_smaller;
  37.         }
  38.     }
  39.    
  40. }
  41. void remove_value(int x, ll & difference_smaller, ll & sum_greater) {
  42.     if(greater_values.find(x) != greater_values.end()) {
  43.         greater_values.erase(greater_values.find(x));
  44.         sum_greater -= x;
  45.     }
  46.     else if(smaller_values.find(x) != smaller_values.end()) {
  47.         smaller_values.erase(smaller_values.find(x));
  48.         difference_smaller += x;
  49.     }
  50.    
  51.     if(smaller_values.empty()) {
  52.         int tmp = *greater_values.begin();
  53.         smaller_values.insert(*greater_values.begin());
  54.         greater_values.erase(greater_values.begin());
  55.        
  56.         difference_smaller -= tmp;
  57.         sum_greater -= tmp;
  58.     }
  59.    
  60. }
  61. int main() {
  62.     ios_base::sync_with_stdio(false);
  63.  
  64.     int n;
  65.     cin >> n >> k;
  66.    
  67.     vector<int> v(n);
  68.     for(int i = 0; i < n; i++) {
  69.         cin >> v[i];
  70.     }
  71.    
  72.     smaller_values.insert(v[0]);
  73.     ll difference_smaller  = -v[0];
  74.     ll sum_greater = 0;
  75.     for(int i = 1; i < k; i++) {
  76.         insert_value(v[i], difference_smaller, sum_greater);
  77.     }
  78.     ll median = *smaller_values.rbegin();
  79.    
  80.     ll tmp = (ll) (smaller_values.size() * median) + difference_smaller;
  81.     tmp += sum_greater - (ll) (greater_values.size() * median);
  82.     cout << tmp << " ";
  83.     for(int i = k; i < n; i++) {
  84.         if(k > 1) {
  85.             remove_value(v[i - k], difference_smaller, sum_greater);
  86.             insert_value(v[i], difference_smaller, sum_greater);
  87.         }
  88.         else {
  89.             insert_value(v[i], difference_smaller, sum_greater);
  90.             remove_value(v[i - 1], difference_smaller, sum_greater);
  91.         }
  92.         median = *smaller_values.rbegin();
  93.         tmp = (ll) (smaller_values.size() * median) + difference_smaller;
  94.         tmp += sum_greater - (ll) (greater_values.size() * median);
  95.         cout << tmp << " ";
  96.        
  97.     }
  98.    
  99.     return 0;
  100. }
  101. // 5, 1, 2, 3
  102. // 1, 2, 3, 5
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement