fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. struct FenwickMax {
  6. int n;
  7. vector<int> bit;
  8.  
  9. FenwickMax(int n = 0) { init(n); }
  10.  
  11. void init(int n_) {
  12. n = n_;
  13. bit.assign(n + 1, 0);
  14. }
  15.  
  16. void update(int idx, int val) {
  17. for (; idx <= n; idx += idx & -idx)
  18. bit[idx] = max(bit[idx], val);
  19. }
  20.  
  21. int query(int idx) {
  22. int res = 0;
  23. for (; idx > 0; idx -= idx & -idx)
  24. res = max(res, bit[idx]);
  25. return res;
  26. }
  27. };
  28.  
  29. int main() {
  30. ios::sync_with_stdio(false);
  31. cin.tie(nullptr);
  32.  
  33. int n, k;
  34. cin >> n >> k;
  35. vector<ll> a(n);
  36. for (int i = 0; i < n; i++) cin >> a[i];
  37.  
  38. auto check = [&](ll x) -> bool {
  39. vector<ll> L(n), R(n);
  40. for (int i = 0; i < n; i++) {
  41. L[i] = a[i] - 1LL * i * x;
  42. R[i] = a[i] + 1LL * i * x;
  43. }
  44.  
  45. vector<ll> comp = R;
  46. sort(comp.begin(), comp.end());
  47. comp.erase(unique(comp.begin(), comp.end()), comp.end());
  48.  
  49. auto getPos = [&](ll v) {
  50. return (int)(lower_bound(comp.begin(), comp.end(), v) - comp.begin()) + 1;
  51. };
  52.  
  53. vector<int> ord(n);
  54. iota(ord.begin(), ord.end(), 0);
  55. sort(ord.begin(), ord.end(), [&](int i, int j) {
  56. if (L[i] != L[j]) return L[i] > L[j];
  57. if (R[i] != R[j]) return R[i] < R[j];
  58. return i < j;
  59. });
  60.  
  61. FenwickMax fw((int)comp.size());
  62. int bestAll = 0;
  63.  
  64. for (int id : ord) {
  65. int pos = getPos(R[id]);
  66. int best = fw.query(pos);
  67. int cur = best + 1;
  68. fw.update(pos, cur);
  69. bestAll = max(bestAll, cur);
  70. }
  71.  
  72. return n - bestAll <= k;
  73. };
  74.  
  75. ll lo = -1, hi = 2000000000LL;
  76. while (hi - lo > 1) {
  77. ll mid = (lo + hi) / 2;
  78. if (check(mid)) hi = mid;
  79. else lo = mid;
  80. }
  81.  
  82. cout << hi << '\n';
  83. return 0;
  84. }
  85.  
Success #stdin #stdout 0.02s 5316KB
stdin
Standard input is empty
stdout
2000000000