zoukankan      html  css  js  c++  java
  • Codeforces-1312E Array Shrinking

    Codeforces-1312E Array Shrinking

    You are given an array (a_1, a_2, dots, a_n). You can perform the following operation any number of times:

    • Choose a pair of two neighboring equal elements (a_i = a_{i + 1}) (if there is at least one such pair).
    • Replace them by one element with value (a_i + 1).

    After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array (a) you can get?

    Input

    The first line contains the single integer (n) ((1 le n le 500)) — the initial length of the array (a).

    The second line contains (n) integers (a_1, a_2, dots, a_n) ((1 le a_i le 1000)) — the initial array (a).

    Output

    Print the only integer — the minimum possible length you can get after performing the operation described above any number of times.

    Examples

    Input

    5
    4 3 2 2 3
    

    Output

    2
    

    Input

    7
    3 3 4 4 4 3 3
    

    Output

    2
    

    Input

    3
    1 3 5
    

    Output

    3
    

    Input

    1
    1000
    

    Output

    1
    

    Note

    In the first test, this is one of the optimal sequences of operations: (4) (3) (2) (2) (3) ( ightarrow) (4) (3) (3) (3) ( ightarrow) (4) (4) (3) ( ightarrow) (5) (3).

    In the second test, this is one of the optimal sequences of operations: (3) (3) (4) (4) (4) (3) (3) ( ightarrow) (4) (4) (4) (4) (3) (3) ( ightarrow) (4) (4) (4) (4) (4) ( ightarrow) (5) (4) (4) (4) ( ightarrow) (5) (5) (4) ( ightarrow) (6) (4).

    In the third and fourth tests, you can't perform the operation at all.

    题意

    给定n个数,可以将相邻两个相同的数a[i]替换为a[i]+1,问序列最短是多少

    题解

    利用区间dp预处理出可以全部消除只留下一个数的区间,具体方法为

    (g[i][j])为区间([i,j])内全部消除所获得的值,初始(g[i][i]=a[i]),区间dp,若(g[l][k]=g[k+1][r],则g[l][r] = g[l][k]+1),同时计入统计区间,这样区间最大为(n^2)个.

    再设(f[i])为前i个数的最小长度,则有以下转移

    对于区间结尾为(i)的区间(j),(f[i]=min(f[i],f[p[j].l-1]+1))

    答案即为(f[n])

    代码

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 505;
    int a[N], g[N][N], f[N];
    struct node {
        int l, r;
        bool operator < (const node &b) {
            return r < b.r;
        }
    } p[N * N];
    int main() {
        int n;
        scanf("%d", &n);
        int tot = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            g[i][i] = a[i];
            p[++tot] = {i, i};
        }
        for (int len = 2; len <= n; len++) {
            for (int l = 1; l <= n - len + 1; l++) {
                int r = l + len - 1;
                for (int k = l; k < r; k++) {
                    if (g[l][k] && g[k + 1][r] && g[l][k] == g[k + 1][r]) {
                        g[l][r] = g[l][k] + 1;
                        p[++tot] = {l, r};
                    }
                }
            }
        }
        sort(p + 1, p + tot + 1);
        int j = 1;
        for (int i = 1; i <= n; i++) {
            f[i] = i;
            while (p[j].r == i) {
                f[i] = min(f[i], f[p[j].l - 1] + 1);
                j++;
            }
        }
        printf("%d
    ", f[n]);
        return 0;
    }
    
  • 相关阅读:
    代码质量管理平台SonarQube的安装、配置与使用
    IntelliJ IDEA优秀插件(编程通用)
    Helpers.parallel_bulk in Python not working?
    把mysql的数据导出成txt
    python从数据库获取全量数据的方法
    ylbtech-LanguageSamples-ExplicitInterface(显示接口)
    ylbtech-LanguageSamples-Events(事件)
    ylbtech-LanguageSamples-Delegates(委托)
    ylbtech-LanguageSamples-ConditionalMethods(条件方法)
    ylbtech-LanguageSamples-CommandLine(命令行参数)
  • 原文地址:https://www.cnblogs.com/artoriax/p/12575013.html
Copyright © 2011-2022 走看看