Advertisement
BaoJIaoPisu

Untitled

Mar 14th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using ld = long double;
  7. using ull = unsigned long long;
  8.  
  9. using pii = pair<int, int>;
  10. using pll = pair<ll, ll>;
  11. using pld = pair<ld, ld>;
  12.  
  13. #define fi first
  14. #define se second
  15. #define pb push_back
  16. #define pf push_front
  17. #define mp make_pair
  18. #define ins insert
  19. #define btpc __builtin_popcount
  20. #define btclz __builtin_clz
  21.  
  22. #define sz(x) (int)(x.size());
  23. #define all(x) x.begin(), x.end()
  24. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  25.  
  26. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  27.  
  28. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  29. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  30. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  31.  
  32. template<class X, class Y>
  33. bool minimize(X &x, const Y &y) {
  34. if (x > y)
  35. {
  36. x = y;
  37. return true;
  38. }
  39. return false;
  40. }
  41. template<class X, class Y>
  42. bool maximize(X &x, const Y &y) {
  43. if (x < y)
  44. {
  45. x = y;
  46. return true;
  47. }
  48. return false;
  49. }
  50.  
  51. const int MOD = 1e9 + 7; //998244353
  52.  
  53. template<class X, class Y>
  54. void add(X &x, const Y &y) {
  55. x = (x + y);
  56. if(x >= MOD) x -= MOD;
  57. }
  58.  
  59. template<class X, class Y>
  60. void sub(X &x, const Y &y) {
  61. x = (x - y);
  62. if(x < 0) x += MOD;
  63. }
  64.  
  65. /* Author : Le Ngoc Bao Anh, 11A5, LQD High School for Gifted Student*/
  66.  
  67. const ull INF = 6e18;
  68. const int N = 6e5 + 10;
  69.  
  70. ull a[N], b[N], d[N], p[N], dp[N];
  71. int q[N];
  72.  
  73. void solve() {
  74. int n, r; ull k;
  75. cin >> n >> r >> k;
  76.  
  77. for(int i = 1; i <= n; i++) {
  78. cin >> a[i];
  79. int lo = max(1, i - r);
  80. int hi = min(n, i + r);
  81. dp[lo] += a[i];
  82. dp[hi + 1] -= a[i];
  83. }
  84.  
  85. for(int i = 1; i <= n; i++) dp[i] = dp[i - 1] + dp[i];
  86. for(int i = 1; i <= n; i++) {
  87. cin >> b[i];
  88. b[i] = b[i] - a[i];
  89. }
  90.  
  91. auto ok = [&](ull v) -> bool {
  92. int cnt = 0;
  93. ull curr = k;
  94. for(int i = 1; i <= n; i++) d[i] = b[i];
  95. for(int i = 1; i <= r; i++) q[++cnt] = i;
  96. for(int i = 1; i <= n + 1; i++) p[i] = 0;
  97.  
  98. for(int i = 1; i <= n; i++) {
  99. if(i + r <= n) q[++cnt] = i + r;
  100. p[i] = p[i] + p[i - 1];
  101.  
  102. while(curr > 0 && cnt > 0 && dp[i] + p[i] < v) {
  103. int id = q[cnt];
  104. if(d[id] == 0) {
  105. --cnt;
  106. continue;
  107. }
  108.  
  109. if(id >= i) {
  110. ull sb = min(d[id], v - dp[i] - p[i]);
  111. sb = min(sb, curr);
  112. d[id] -= sb;
  113. curr -= sb;
  114. p[i] += sb;
  115. p[min(n, id + r) + 1] -= sb;
  116. } else {
  117. if(id + r < i) break;
  118. ull sb = min(d[id], v - dp[i] - p[i]);
  119. sb = min(sb, curr);
  120. d[id] -= sb;
  121. curr -= sb;
  122. p[i] += sb;
  123. p[min(n, id + r) + 1] -= sb;
  124. }
  125. }
  126.  
  127. if(dp[i] + p[i] < v) return false;
  128. }
  129.  
  130. return true;
  131. };
  132.  
  133. ull L = 0, R = INF, ans = 0;
  134. while(L <= R) {
  135. ull mid = (L + R) >> 1;
  136. if(ok(mid)) {
  137. ans = mid;
  138. L = mid + 1;
  139. } else R = mid - 1;
  140. }
  141.  
  142. cout << ans;
  143. }
  144.  
  145. int main()
  146. {
  147. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  148. #ifndef ONLINE_JUDGE
  149. freopen("input.txt", "r", stdin);
  150. freopen("output.txt", "w", stdout);
  151. #else
  152. //online
  153. #endif
  154.  
  155. int tc = 1, ddd = 0;
  156. // cin >> tc;
  157. while(tc--) {
  158. //ddd++;
  159. //cout << "Case #" << ddd << ": ";
  160. solve();
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement