G. Paint Charges

G. Paint Charges

A horizontal grid strip of $n$ cells is given. In the $i$-th cell, there is a paint charge of size $a_i$. This charge can be:

  • either used to the left — then all cells to the left at a distance less than $a_i$ (from $\max(i - a_i + 1, 1)$ to $i$ inclusive) will be painted,
  • or used to the right — then all cells to the right at a distance less than $a_i$ (from $i$ to $\min(i + a_i - 1, n)$ inclusive) will be painted,
  • or not used at all.

Note that a charge can be used no more than once (that is, it cannot be used simultaneously to the left and to the right). It is allowed for a cell to be painted more than once.

What is the minimum number of times a charge needs to be used to paint all the cells of the strip?

Input

The first line of the input contains an integer $t$ ($1 \le t \le 100$) — the number of test cases in the test. This is followed by descriptions of $t$ test cases.

Each test case is specified by two lines. The first one contains an integer $n$ ($1 \le n \le 100$) — the number of cells in the strip. The second line contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the size of the paint charge in the $i$-th cell from the left of the strip.

It is guaranteed that the sum of the values of $n$ in the test does not exceed $1000$.

Output

For each test case, output the minimum number of times the charges need to be used to paint all the cells of the strip.

Example

input

13
1
1
2
1 1
2
2 1
2
1 2
2
2 2
3
1 1 1
3
3 1 2
3
1 3 1
7
1 2 3 1 2 4 2
7
2 1 1 1 2 3 1
10
2 2 5 1 6 1 8 2 8 2
6
2 1 2 1 1 2
6
1 1 4 1 3 2

output

1
2
1
1
1
3
1
2
3
4
2
3
3

Note

In the third test case of the example, it is sufficient to use the charge from the $1$-st cell to the right, then it will cover both cells $1$ and $2$.

In the ninth test case of the example, you need to:

  • use the charge from the $3$-rd cell to the left, covering cells from the $1$-st to the $3$-rd;
  • use the charge from the $5$-th cell to the left, covering cells from the $4$-th to the $5$-th;
  • use the charge from the $7$-th cell to the left, covering cells from the $6$-th to the $7$-th.

In the eleventh test case of the example, you need to:

  • use the charge from the $5$-th cell to the right, covering cells from the $5$-th to the $10$-th;
  • use the charge from the $7$-th cell to the left, covering cells from the $1$-st to the $7$-th.

 

解题思路

  官方题解给出的 dp 状态定义不好直接想到,这里先给出容易想到但有问题的做法,然后再引出官方题解的做法。

  定义 $f(i,j)$ 表示所有由前 $i$ 个位置将前缀 $1 \sim j$ 都涂上色的方案中操作次数的最小值。根据对第 $i+1$ 个位置执行的操作将 $f(i,j)$ 转移到 $i+1$ 对应的状态。

  • 如果第 $i+1$ 个位置不执行操作,那么显然有 $f(i,j) \xrightarrow{0} f(i+1, j)$。
  • 如果第 $i+1$ 个位置执行向左涂色的操作,只有当 $j \geq i+1-a_{i+1}$,有 $f(i,j) \xrightarrow{1} f(i+1, \max\{{i+1,j}\})$。
  • 如果第 $i+1$ 个位置执行向右涂色的操作,只有当 $j \geq i$,有 $f(i,j) \xrightarrow{1} f(i+1, \max\{{i+a_{i+1},j}\})$。

  最后的答案就是 $f(n,n)$。如果你按照这个做法来做那么就会发现题目倒数第 $3$ 个样例跑出的答案是 $3$,实际上应该选择第 $5$ 个位置向右涂色,第 $7$ 个位置向左涂色。由于状态的定义要保证前缀被涂色,因此枚举到 $i+1=5$ 时,位置 $1 \sim 4$ 没被涂色而从位置 $5$ 向右涂色这种状态是不存在的。

  因此可以知道在枚举 $i$ 的过程中不一定要保证前缀被涂色,同时被涂色的区间不一定只有连续的一段。对于这样的状态,就可以引出官方题解中给出的状态定义,即只关心此时最靠左没被涂色的位置,和最靠右已被涂色的位置。

  定义 $f(i,j,k)$ 表示所有由前 $i$ 个位置使得最靠左没被涂色的位置为 $j$,最靠右已涂色的位置为 $k$ 的方案中操作次数的最小值。状态转移方程为:

  • 如果第 $i+1$ 个位置不执行操作,那么显然有 $f(i,j.k) \xrightarrow{0} f(i+1, j, k)$。
  • 如果第 $i+1$ 个位置执行向左涂色的操作,分两种情况。如果 $j < i+1-a_{i+1}-1$,则涂色后最靠左没被涂色的位置还是 $j$,最靠右被涂色的位置取决于 $k$ 和 $i+1$。因此有 $f(i,j,k) \xrightarrow{1} f(i+1, j, \max\{{k,i+1}\})$。否则如果 $j \geq i+1-a_{i+1}-1$,继续分两种情况:$k < i+1$,显然涂色后最靠左没被涂色的位置为 $i+2$,最靠右已涂色的位置为 $i+1$,因此有 $f(i,j,k) \xrightarrow{1} f(i+1, i+2, i+1)$。$k \geq i+1$,由于是通过前 $i$ 个位置使得第 $k$ 个位置被涂色,因此区间 $[i, k]$ 一定都被涂色,所以涂色后最靠左没被涂色的位置为 $k+1$,最靠右已涂色的位置为 $k$,有 $f(i,j,k) \xrightarrow{1} f(i+1, k+1, k)$。两种情况综合一下就有 $f(i,j,k) \xrightarrow{1} f(i+1, \max\{{k,i+1}\}+1, \max\{{k,i+1}\})$。
  • 如果第 $i+1$ 个位置执行向右涂色的操作,同理分两种情况。如果 $j < i+1$,则有 $f(i,j,k) \xrightarrow{1} f(i+1,j, \max\{{k, i+a_{i+1}}\})$。否则如果 $j \geq i+1$,则有 $f(i,j,k) \xrightarrow{1} f(i+1, \max\{{k, i+a_{i+1}}\}+1, \max\{{k, i+a_{i+1}}\})$。

  AC 代码如下,时间复杂度为 $O(n^3)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 110;

int a[N];
int f[N][N][N];

void solve() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", a + i);
    }
    memset(f, 0x3f, sizeof(f));
    f[0][1][0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 1; j <= n + 1; j++) {
            for (int k = 0; k <= n; k++) {
                f[i + 1][j][k] = min(f[i + 1][j][k], f[i][j][k]);
                if (j < i + 1 - a[i + 1] + 1) {
                    int &v = f[i + 1][j][max(k, i + 1)];
                    v = min(v, f[i][j][k] + 1);
                }
                else {
                    int &v = f[i + 1][max(k, i + 1) + 1][max(k, i + 1)];
                    v = min(v, f[i][j][k] + 1);
                }
                if (j < i + 1) {
                    int &v = f[i + 1][j][min(n, max(k, i + a[i + 1]))];
                    v = min(v, f[i][j][k] + 1);
                }
                else {
                    int &v = f[i + 1][min(n, max(k, i + a[i + 1])) + 1][min(n, max(k, i + a[i + 1]))];
                    v = min(v, f[i][j][k] + 1);
                }
            }
        }
    }
    printf("%d\n", f[n][n + 1][n]);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 923 (Div. 3) Editorial:https://codeforces.com/blog/entry/125597

  Codeforces Round 923 (Div. 3) A-G:https://zhuanlan.zhihu.com/p/681725397

  Codeforces Round 923 (Div. 3) A - G:https://zhuanlan.zhihu.com/p/681721421

posted @ 2024-02-09 19:14  onlyblues  阅读(77)  评论(0编辑  收藏  举报
Web Analytics