fork download
  1. #include <bits/stdc++.h>
  2.  
  3. #define debug cout << "ok\n";
  4. #define SQR(x) (1LL * ((x) * (x)))
  5. #define MASK(i) (1LL << (i))
  6. #define BIT(x, i) (((x) >> (i)) & 1)
  7. #define fi first
  8. #define se second
  9. #define pb push_back
  10.  
  11. #define mp make_pair
  12. #define pii pair<int,int>
  13. #define pli pair<ll,int>
  14. #define vi vector<int>
  15.  
  16. #define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  17.  
  18. typedef long long ll;
  19. typedef unsigned long long ull;
  20. typedef long double ld;
  21. typedef unsigned int ui;
  22.  
  23. using namespace std;
  24.  
  25. const int M = 1e9 + 7;
  26. const int INF = 1e9 + 7;
  27. const ll INFLL = (ll)2e18 + 7LL;
  28. const ld PI = acos(-1);
  29.  
  30. const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
  31. const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
  32.  
  33. template<class _, class __>
  34. bool minimize(_ &x, const __ y){
  35. if(x > y){
  36. x = y;
  37. return true;
  38. } else return false;
  39. }
  40. template<class _, class __>
  41. bool maximize(_ &x, const __ y){
  42. if(x < y){
  43. x = y;
  44. return true;
  45. } else return false;
  46. }
  47.  
  48. template<class _,class __>
  49. void Add(_ &x, const __ y) {
  50. x += y;
  51. if (x >= M) {
  52. x -= M;
  53. }
  54. return;
  55. }
  56.  
  57. template<class _,class __>
  58. void Diff(_ &x, const __ y) {
  59. x -= y;
  60. if (x < 0) {
  61. x += M;
  62. }
  63. return;
  64. }
  65.  
  66. //--------------------------------------------------------------
  67.  
  68. const int MaxN = 1e6+7;
  69.  
  70. int n,l,r,k;
  71. int a[MaxN],f[MaxN];
  72. ll b[MaxN];
  73.  
  74. bool ch(int cur1,int cur2) {
  75. if (f[cur1] != f[cur2]) return f[cur1] > f[cur2];
  76. else return b[cur1] < b[cur2];
  77. }
  78.  
  79. bool check(int val) {
  80. for (int i=1;i<=n;i++) b[i] = a[i] - val;
  81. for (int i=1;i<=n;i++) f[i] = INFLL;
  82. for (int i=1;i<=n;i++) b[i] += b[i-1];
  83. deque<int> q;
  84. for (int i=l;i<=n;i++) {
  85. while (!q.empty() && ch(q.back(),i-l)) q.pop_back();
  86. while (!q.empty() && q.front() < i - r) q.pop_front();
  87. q.pb(i-l);
  88. if (b[i] >= b[q.front()]) f[i] = f[q.front()] + 1;
  89. else f[i] = f[q.front()];
  90. }
  91. return f[n] >= k && f[n] < INFLL;
  92. }
  93.  
  94. void sol() {
  95. cin >> n >> l >> r >> k;
  96. for (int i=1;i<=n;i++) cin >> a[i];
  97. int l = 1,r = 1e6;
  98. int res = 0;
  99. while (l <= r) {
  100. int mid = (l+r) >> 1;
  101. if (check(mid)) {
  102. res = mid;
  103. l = mid + 1;
  104. }
  105. else r = mid - 1;
  106. }
  107. cout << res;
  108. }
  109.  
  110. int main() {
  111. // freopen("test.inp","r",stdin);
  112. // freopen("test.out","w",stdout);
  113. FAST
  114. int t=1;
  115. // cin >> t;
  116. while (t--) sol();
  117. }
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
1000000