Block Sequence
题面翻译
给出一个长度为n的整数序列a。定义一个合法序列,当且仅当该序列由一系列块组成(每个块由两部分构成,首先第一个数字是块中元素的数量,紧接着是这些元素)。在一次操作中,你可以删除序列中任意一个元素。问你至少需要多少次操作使这个序列变为一个合法序列?
题目描述
Given a sequence of integers $ a $ of length $ n $ .
A sequence is called beautiful if it has the form of a series of blocks, each starting with its length, i.e., first comes the length of the block, and then its elements. For example, the sequences [ $ \color{red}{3},\ \color{red}{3},\ \color{red}{4},\ \color{red}{5},\ \color{green}{2},\ \color{green}{6},\ \color{green}{1} $ ] and [ $ \color{red}{1},\ \color{red}{8},\ \color{green}{4},\ \color{green}{5},\ \color{green}{2},\ \color{green}{6},\ \color{green}{1} $ ] are beautiful (different blocks are colored differently), while [ $ 1 $ ], [ $ 1,\ 4,\ 3 $ ], [ $ 3,\ 2,\ 1 $ ] are not.
In one operation, you can remove any element from the sequence. What is the minimum number of operations required to make the given sequence beautiful?
输入格式
The first line of the input contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases. Then follows the descriptions of the test cases.
The first line of each test case contains a single integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the length of the sequence $ a $ .
The second line of each test case contains $ n $ integers $ a_1, a_2, \dots, a_n $ ( $ 1 \le a_i \le 10^6 $ ) — the elements of the sequence $ a $ .
It is guaranteed that the sum of the values of $ n $ over all test cases does not exceed $ 2 \cdot 10^5 $ .
输出格式
For each test case, output a single number — the minimum number of deletions required to make the sequence $ a $ beautiful.
样例 #1
样例输入 #1
7
7
3 3 4 5 2 6 1
4
5 6 3 2
6
3 4 1 6 7 7
3
1 4 3
5
1 2 3 4 5
5
1 2 3 1 2
5
4 5 5 1 5
样例输出 #1
0
4
1
1
2
1
0
提示
In the first test case of the example, the given sequence is already beautiful, as shown in the statement.
In the second test case of the example, the sequence can only be made beautiful by removing all elements from it.
In the fifth test case of the example, the sequence can be made beautiful by removing the first and last elements. Then the sequence will become [ $ 2,\ 3,\ 4 $ ].
每一个数有两种选择,删和不删,删,就要接上前一个,不删就要接上前一线段
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], dp[N];
int main() {
int T; cin >> T;
while(T--) {
int n; cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
dp[n + 1] = 0;
if(a[n] == 0) dp[n] = 0;
else dp[n] = 1;
for (int i = n - 1; i >= 1; i--) {
//倒序
if(a[i] + i <= n)
dp[i] = min(dp[i + 1] + 1, dp[i + a[i] + 1]);
else dp[i] = dp[i + 1] + 1;
//dp
}
cout << dp[1] << endl;
}
return 0;
}