csuoj-1729-齿轮传动

Description

你在一家机械厂打工,你的老板让你把一组齿轮种类序列a1,a2,..,an取走几个让齿轮的传动比为1:1,老板要求你取走最少的齿轮,不能改变齿轮原来的相对位置,满足条件,即齿轮种类组合起来是回文串。

Input

多组数据,第一行有一个整数T, 表示有T组数据。(T<=100)

以下每组数据第一行有一个整数n, 表示n个齿轮(1<=n<=1000)

接下来一行有n个整数a1,a2,…,an表示齿轮种类(1<=ai<=10000)

Output

取走的最少齿轮数

Sample Input

1
4
1 2 3 1

Sample Output

1

HINT



这是一道模板题,看了题就知道了

然后对着模板一敲

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int num[maxn], dp[maxn][maxn];
int n;
 
int main(){
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int T;
    scanf("%d", &T);
    while (T--){
        scanf("%d", &n);
        for (int i=0; i<n; ++i)
            scanf("%d", num+i);
        memset(dp, 0, sizeof(dp));
        for (int i=n-1; i>=0; --i){
            dp[i][i] = 1;
            for (int j=i+1; j<n; ++j)
                if (num[i] == num[j])
                    dp[i][j] = dp[i+1][j-1]+2;
                else
                    dp[i][j] = max(dp[i][j-1], dp[i+1][j]);
        }
        printf("%d\n", n-dp[0][n-1]);
    }
    return 0;
}


你可能感兴趣的:(csuoj-1729-齿轮传动)