fork download
  1. /*
  2. * @Author: hungeazy
  3. * @Date: 2024-11-23 21:48:16
  4. * @Last Modified by: hungeazy
  5. * @Last Modified time: 2024-11-23 21:53:41
  6. */
  7. #include <bits/stdc++.h>
  8. #include <ext/pb_ds/assoc_container.hpp>
  9. #include <ext/pb_ds/tree_policy.hpp>
  10. // #pragma GCC optimize("O3")
  11. // #pragma GCC optimize("unroll-loops")
  12. // #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
  13. using namespace std;
  14. using namespace __gnu_pbds;
  15. #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  16. #define int long long
  17. #define ll long long
  18. #define ull unsigned long long
  19. #define sz(x) x.size()
  20. #define sqr(x) (1LL * (x) * (x))
  21. #define all(x) x.begin(), x.end()
  22. #define fill(f,x) memset(f,x,sizeof(f))
  23. #define FOR(i,l,r) for(int i=l;i<=r;i++)
  24. #define FOD(i,r,l) for(int i=r;i>=l;i--)
  25. #define debug(x) cout << #x << " = " << x << '\n'
  26. #define ii pair<int,int>
  27. #define iii pair<int,ii>
  28. #define di pair<ii,ii>
  29. #define vi vector<int>
  30. #define vii vector<ii>
  31. #define mii map<int,int>
  32. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  33. #define fi first
  34. #define se second
  35. #define pb push_back
  36. #define MOD 1000000007
  37. #define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
  38. #define YES cout << "YES\n"
  39. #define NO cout << "NO\n"
  40. #define MASK(i) (1LL << (i))
  41. #define c_bit(i) __builtin_popcountll(i)
  42. #define BIT(x,i) ((x) & MASK(i))
  43. #define SET_ON(x,i) ((x) | MASK(i))
  44. #define SET_OFF(x,i) ((x) & ~MASK(i))
  45. #define oo 1e18
  46. #define name ""
  47. #define endl '\n'
  48. #define time() cerr << endl << "-------------Time:" << 1000.0 * clock() / CLOCKS_PER_SEC << "ms.";
  49. template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
  50. template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
  51. const int N = (int)4e6+10;
  52. int a[N],n;
  53.  
  54. namespace hungeazy {
  55.  
  56. int l[N],r[N],dp[N],trace[N];
  57.  
  58. void TRACE(int i)
  59. {
  60. if (i < 1) return;
  61. if (trace[i] == i) TRACE(l[i]-1), cout << -i << " ";
  62. else TRACE(trace[i]-1), cout << trace[i] << " ";
  63. }
  64.  
  65. void solve(void)
  66. {
  67. FOR(i,1,n) l[i] = r[i] = dp[i] = trace[i] = i;
  68. stack<int> st;
  69. st.push(1);
  70. FOR(i,2,n)
  71. {
  72. while (!st.empty() and i-a[i] < st.top())
  73. {
  74. minimize(l[i],l[st.top()]);
  75. st.pop();
  76. }
  77. if (!st.empty() and i-a[i] < st.top()) minimize(l[i],l[st.top()]);
  78. st.push(i);
  79. }
  80. while (!st.empty()) st.pop();
  81. st.push(n);
  82. FOD(i,n-1,1)
  83. {
  84. while (!st.empty() and i+a[i] > st.top())
  85. {
  86. maximize(r[i],r[st.top()]);
  87. st.pop();
  88. }
  89. if (!st.empty() and i+a[i] > st.top()) maximize(r[i],r[st.top()]);
  90. st.push(i);
  91. }
  92. priority_queue<ii,vii,greater<ii>> q;
  93. FOR(i,1,n)
  94. {
  95. if (dp[i] > dp[l[i]-1]+1)
  96. {
  97. dp[i] = dp[l[i]-1]+1;
  98. trace[i] = i;
  99. }
  100. while (!q.empty() and i > r[q.top().se]) q.pop();
  101. if (!q.empty() and dp[i] > dp[q.top().se-1]+1)
  102. {
  103. trace[i] = q.top().se;
  104. dp[i] = dp[q.top().se-1]+1;
  105. }
  106. q.push({dp[i-1],i});
  107. }
  108. cout << dp[n] << endl;
  109. TRACE(n);
  110. }
  111.  
  112. }
  113.  
  114. signed main()
  115. {
  116. fast;
  117. if (fopen(name".inp","r"))
  118. {
  119. freopen(name".inp","r",stdin);
  120. freopen(name".out","w",stdout);
  121. }
  122. cin >> n;
  123. FOR(i,1,n) cin >> a[i];
  124. hungeazy::solve();
  125. time();
  126. return 0;
  127. }
  128. // ██░ ██ █ ██ ███▄ █ ▄████
  129. //▓██░ ██▒ ██ ▓██▒ ██ ▀█ █ ██▒ ▀█▒
  130. //▒██▀▀██░▓██ ▒██░▓██ ▀█ ██▒▒██░▄▄▄░
  131. //░▓█ ░██ ▓▓█ ░██░▓██▒ ▐▌██▒░▓█ ██▓
  132. //░▓█▒░██▓▒▒█████▓ ▒██░ ▓██░░▒▓███▀▒
  133. // ▒ ░░▒░▒░▒▓▒ ▒ ▒ ░ ▒░ ▒ ▒ ░▒ ▒
  134. // ▒ ░▒░ ░░░▒░ ░ ░ ░ ░░ ░ ▒░ ░ ░
  135. // ░ ░░ ░ ░░░ ░ ░ ░ ░ ░ ░ ░ ░
  136. // ░ ░ ░ ░ ░ ░
  137.  
Success #stdin #stdout #stderr 0.01s 5316KB
stdin
Standard input is empty
stdout
0
stderr
-------------Time:6.199ms.