fork download
  1. //ZT. Tấn và cú nhảy thần sầu
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define ll long long
  6. #define For(i, a, b) for(int i = a; i <= b; ++i)
  7. #define endl '\n'
  8.  
  9. const int maxn = 1e5 + 5;
  10.  
  11. int n, dp[maxn], bit1[maxn], bit2[maxn];
  12. ll d, h[maxn];
  13. vector<ll> comp;
  14.  
  15. void update(int bit[], int i, int val)
  16. {
  17. int m = comp.size();
  18. for(; i <= m; i += i & -i) bit[i] = max(bit[i], val);
  19. }
  20.  
  21. int get(int bit[], int i)
  22. {
  23. int res = 0;
  24. for(; i > 0; i -= i & -i) res = max(res, bit[i]);
  25. return res;
  26. }
  27.  
  28. signed main()
  29. {
  30. ios_base::sync_with_stdio(false);
  31. cin.tie(NULL); cout.tie(NULL);
  32.  
  33. // freopen("ZT.INP", "r", stdin);
  34. // freopen("ZT.OUT", "w", stdout);
  35.  
  36. cin >> n >> d;
  37.  
  38. For(i, 1, n)
  39. {
  40. cin >> h[i];
  41. comp.push_back(h[i]);
  42. }
  43.  
  44. sort(comp.begin(), comp.end());
  45. comp.erase(unique(comp.begin(), comp.end()), comp.end());
  46.  
  47. int m = comp.size();
  48. int ans = 0;
  49.  
  50. For(i, 1, n)
  51. {
  52. int best = 0;
  53.  
  54. int p1 = upper_bound(comp.begin(), comp.end(), h[i] - d) - comp.begin();
  55.  
  56. if(p1 > 0)
  57. best = max(best, get(bit1, p1));
  58.  
  59. int p2 = lower_bound(comp.begin(), comp.end(), h[i] + d) - comp.begin();
  60.  
  61. if(p2 < m)
  62. best = max(best, get(bit2, m - p2));
  63.  
  64. dp[i] = best + 1;
  65.  
  66. int id = lower_bound(comp.begin(), comp.end(), h[i]) - comp.begin() + 1;
  67.  
  68. update(bit1, id, dp[i]);
  69. update(bit2, m - id + 1, dp[i]);
  70.  
  71. ans = max(ans, dp[i]);
  72. }
  73.  
  74. cout << ans;
  75. }
  76.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty