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;
    }
    
  • 相关阅读:
    我认知的javascript之函数调用
    table 的宽度设置无效
    【转】微信读书排版引擎自动化测试方案
    RocksDB原理及应用
    ElasticSearch 架构及应用简介
    芝加哥大学论文写作指南 简述
    python 常用模块
    Flask-SQLAlchemy安装及设置
    selenium设置程序失败后重启
    获取临时IP加入临时列表使用
  • 原文地址:https://www.cnblogs.com/artoriax/p/12575013.html
Copyright © 2011-2022 走看看