令 \(dp[i][x]\) 表示 \(j(j<i)\)&&\((j+a_j>=i)\)是到\(i\)唯一途径,且往后能覆盖到\(x\)\((x>=i)\)
即唯一通过\(j\)跳转到\(i\)节点,且\(j\)往后能跳转到至多\(i+x\)。
又由于路径具有唯一性,并且总是存在。我们总可以找到一个唯一节点跳转到当前节点。
转移方程就有:
\(dp[i][j+a[j]]=dp[j][i-1]+cnt\)即当前\(dp[i]\)能过往后至多被转移到\(j+a[j]\),且\(j\)和\(i-1\)之间,有\(cnt\)个能够达到\(i\)的节点需要被清除。并且\(dp[j][i-1]\)表达了到达\(j\)的唯一性。故该方程能表达达到\(i\)节点的唯一性。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
int T, n;
const int N = 3e3 + 10;
int a[N];
int dp[N][N];
int main() {
T = read();
while (T--) {
n = read();
upd(i, 1, n) {
a[i] = read();
}
upd(i, 1, n) {
upd(j, 1, n) {
dp[i][j] = INF;
}
}
upd(i, 1, n)dp[1][i] = 0;
upd(i, 2, n) {
int cnt = 0;
dwd(j, i-1, 1) {
if (a[j] + j >= i) {
dp[i][a[j] + j] = min(dp[j][i - 1] + cnt, dp[i][a[j] + j]);
cnt++;
}
}
upd(j, i + 1, n) {
dp[i][j] = min(dp[i][j], dp[i][j - 1]);
}
}
printf("%d\n", dp[n][n]);
}
return 0;
}