ssrtatarin

Untitled

Jul 8th, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. #define ll long long
  6. #define int long long
  7. const int inf = 1e9;
  8.  
  9. ll M2 = 1e9 + 9, M1 = 1e9 + 7;
  10. //mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  11.  
  12. void solve() {
  13.     int n;
  14.     cin >> n;
  15.     vector<int> a(n);
  16.     for (auto &x : a) {
  17.         cin >> x;
  18.     }
  19.     vector<int> dp(n, inf);
  20.     dp[0] = 1;
  21.     for (int i = 0; i < n; ++i) {
  22.         if (i > 0) {
  23.             dp[i] = min(dp[i], dp[i - 1] + 1);
  24.         }
  25.         if (i + a[i] < n) {
  26.             dp[i + a[i]] = min(dp[i + a[i]], (i == 0 ? 0 : dp[i - 1]));
  27.         }
  28.     }
  29.     cout << dp[n - 1] << '\n';
  30.  
  31. }
  32.  
  33. signed main() {
  34.  
  35.  
  36.     ios_base::sync_with_stdio(false);
  37.     cin.tie(nullptr);
  38.     ll thrush = 1;
  39.     cin >> thrush;
  40.     while (thrush--) { solve();}
  41. }
  42.  
Add Comment
Please, Sign In to add comment