Advertisement
SorahISA

[NHSPC 2019] E. 水槽

Apr 20th, 2020
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. // #pragma GCC target("avx2")
  2. #pragma GCC optimize("O3", "unroll-loops")
  3.  
  4. // #include <bits/extc++.h>
  5. // using namespace __gnu_pbds;
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. #define int long long
  11. #define double long double
  12. // template <typename T>
  13. // using pbds_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  14. using pii = pair<int, int>;
  15. template<typename T>
  16. using prior = priority_queue<T, vector<T>, greater<T>>;
  17. template<typename T>
  18. using Prior = priority_queue<T>;
  19.  
  20. #define X first
  21. #define Y second
  22. #define ALL(x) (x).begin(), (x).end()
  23. #define eb emplace_back
  24. #define pb push_back
  25.  
  26. #define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
  27. #define RANDOM() random_device __rd; \
  28.                  mt19937 __gen = mt19937(__rd()); \
  29.                  uniform_int_distribution<int> __dis(0, 1); \
  30.                  auto rnd = bind(__dis, __gen);
  31.  
  32. const int INF = 1E18;
  33. const int mod = 1E9 + 7;
  34. const int maxn = 1 << 17;
  35. const int lgmx = 17;
  36.  
  37. int n, p, water;
  38. int hei[maxn], ans[maxn], sparse[maxn][lgmx];
  39.  
  40. int findMax(int L, int R) {
  41.     int lay = __lg(R-L+1);
  42.     return hei[sparse[L][lay]] > hei[sparse[R-(1<<lay)+1][lay]] ?
  43.            sparse[L][lay] : sparse[R-(1<<lay)+1][lay];
  44. }
  45.  
  46. void recur(int L, int R, int in, int left) {
  47.     if (L == R) {ans[L] = left; return;}
  48.    
  49.     int pos = findMax(L+1, R);
  50.     // cout << "findMax " << L+1 << " " << R << " , " << pos << " ";
  51.     // cout << "left : " << left << "\n";
  52.    
  53.     if ((R-L+1) * hei[pos] <= left) {
  54.         fill(ans + L, ans + R + 1, left / (R-L+1));
  55.         return;
  56.     }
  57.    
  58.     if (in >= pos) {
  59.         if ((R-pos+1) * hei[pos] <= left) {
  60.             fill(ans + pos, ans + R + 1, hei[pos]);
  61.             recur(L, pos-1, pos-1, left - (R-pos+1) * hei[pos]);
  62.         }
  63.         else {
  64.             recur(pos, R, in, left);
  65.         }
  66.     }
  67.     else {
  68.         if ((pos-L) * hei[pos] <= left) {
  69.             fill(ans + L, ans + pos, hei[pos]);
  70.             recur(pos, R, pos, left - (pos-L) * hei[pos]);
  71.         }
  72.         else {
  73.             recur(L, pos-1, in, left);
  74.         }
  75.     }
  76. }
  77.  
  78. int32_t main() {
  79.     fastIO();
  80.    
  81.     cin >> n >> p >> water;
  82.    
  83.     for (int i = 0; i < n; ++i) cin >> hei[i];
  84.    
  85.     for (int i = 0; i < n; ++i) sparse[i][0] = i;
  86.     for (int lay = 1; lay < lgmx; ++lay) {
  87.         for (int i = 0; i < n-(1<<lay-1); ++i) {
  88.             sparse[i][lay] =
  89.                 hei[sparse[i][lay-1]] > hei[sparse[i+(1<<lay-1)][lay-1]] ?
  90.                 sparse[i][lay-1] : sparse[i+(1<<lay-1)][lay-1];
  91.         }
  92.     }
  93.    
  94.     recur(0, n-2, p, water);
  95.    
  96.     for (int i = 0; i < n-1; ++i) cout << ans[i] << " \n"[i == n-2];
  97.    
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement