zoukankan      html  css  js  c++  java
  • USACO 262144

    USACO 262144

    洛谷传送门

    JDOJ传送门

    Description

    Bessie likes downloading games to play on her cell phone, even though she does find the small touch screen rather cumbersome to use with her large hooves.

    She is particularly intrigued by the current game she is playing. The game starts with a sequence of N positive integers (2≤N≤262,144), each in the range 1…40. In one move, Bessie can take two adjacent numbers with equal values and replace them a single number of value one greater (e.g., she might replace two adjacent 7s with an 8). The goal is to maximize the value of the largest number present in the sequence at the end of the game. Please help Bessie score as highly as possible!

    Input

    The first line of input contains N, and the next N lines give the sequence of N numbers at the start of the game.

    Output

    Please output the largest integer Bessie can generate.

    Sample Input

    4 1 1 1 2

    Sample Output

    3

    HINT

    In this example shown here, Bessie first merges the second and third 1s to obtain the sequence 1 2 2, and then she merges the 2s into a 3. Note that it is not optimal to join the first two 1s.


    最优解声明:


    题解:

    区间DP的一个变形。

    首先262144这个数肯定不同凡响,教给我们一个新常数。

    它是2的18次幂呀。

    所以,它的最优秀的合并,答案也绝对不能超过40+18=58.

    最优秀的合并就是每两个数合并,然后合并出的两个数又能再合并,像二叉树一样地一层层往上合并。

    那么区间DP的复杂度是承载不了这个数据范围的,我们考虑优化。

    我们不应该把区间DP想象的那么狭义。

    定义(f[i][j])表示左端点为j,能拼出i这个数字的右端点位置。

    这样的话,转移方程就是:

    [f[i][j]=f[i-1][f[i-1][j]] ]

    比较好理解吧。

    然后是代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int N=262145,M=60;
    int a[N],f[M][N],ans;
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            f[a[i]][i]=i+1;
        }
        for(int i=1;i<=58;i++)  
            for(int j=1;j<=n;j++)
            {
                f[i][j]=(!f[i][j]?f[i-1][f[i-1][j]]:f[i][j]);
                ans=(f[i][j]?i:ans);
            }
        printf("%d",ans);
        return 0;
    }
    
  • 相关阅读:
    史上最全的浏览器 CSS & JS Hack 手册
    JavaScript1.6数组新特性和JQuery的几个工具方法
    用jquery循环map
    javascript强大的日期函数
    用 javascript 判断 IE 版本号
    常见排序算法基于JS的实现
    JavaScript中callee,caller,argument的理解
    apply()方法和call()方法
    虽然我们可能不想对元素应用3D变换,可我们一样可以开启3D引擎
    在移动端上加上代码,让字体变得平滑
  • 原文地址:https://www.cnblogs.com/fusiwei/p/13813793.html
Copyright © 2011-2022 走看看