csuoj-1729-Gear Drive

Source: Internet
Author: User

Description

You work in a machinery factory, your boss let you put a set of gear type sequence A1,A2,.., an take a few gear transmission ratio of 1:1, the boss asked you to take the least gear, can not change the original relative position of the gear, meet the conditions, that is, gear type combination is palindrome string. Input

Multiple sets of data, the first line has an integer t, which indicates that there is a T group of data. (t<=100)

The first line of each of the following data sets has an integer n, which indicates n gears (1<=n<=1000)

The next line has n integers a1,a2,..., an indicates the gear type (1<=ai<=10000) Output

Minimum number of gears taken from Sample Input

1
4
1 2 3 1
Sample Output
1
HINT


This is a template problem, read the question will know

and knock on the template.

#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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.