zoukankan      html  css  js  c++  java
  • 「USACO16OPEN」「LuoguP3146」248(区间dp

    题目描述

    Bessie likes downloading games to play on her cell phone, even though she doesfind 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 NNN positive integers (2≤N≤2482 leq Nleq 2482N248), each in the range 1…401 ldots 40140. In one move, Bessie cantake two adjacent numbers with equal values and replace them a singlenumber of value one greater (e.g., she might replace two adjacent 7swith an 8). The goal is to maximize the value of the largest numberpresent in the sequence at the end of the game. Please help Bessiescore as highly as possible!

    给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-40),问最大能合出多少。注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3。

    输入输出格式

    输入格式:

    The first line of input contains NNN, and the next NNN lines give the sequence

    of NNN numbers at the start of the game.

    输出格式:

    Please output the largest integer Bessie can generate.

    输入输出样例

    输入样例#1: 复制
    4
    1
    1
    1
    2
    输出样例#1: 复制
    3

    说明

    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。

    设f[i][j]为从i到j全部合起来的最大值,枚举断点k,转移方程:

    if(f[i][k]==f[k+1][j])
    f[i][j]=f[i][k]+1;

    然后注意一下for循环的嵌套顺序就星了。

     1 /*
     2     qwerta
     3     P3146 [USACO16OPEN]248
     4     Accepted
     5     100
     6     代码 C++,0.5KB
     7     提交时间 2018-09-18 16:19:46
     8     耗时/内存
     9     115ms, 1040KB
    10 */
    11 #include<cmath>
    12 #include<cstdio>
    13 #include<iostream>
    14 using namespace std;
    15 int f[257][257];
    16 int main()
    17 {
    18     //freopen("a.in","r",stdin);
    19     int n;
    20     scanf("%d",&n);
    21     for(int i=1;i<=n;++i)
    22     scanf("%d",&f[i][i]);
    23     for(int i=n-1;i;--i)
    24     for(int j=i+1;j<=n;++j)
    25     {
    26         for(int k=i;k<j;++k)
    27           if(f[i][k]==f[k+1][j])
    28             f[i][j]=f[i][k]+1;
    29     }
    30     int ans=0;
    31     for(int i=1;i<=n;++i)
    32     for(int j=i;j<=n;++j)
    33     ans=max(ans,f[i][j]);
    34     cout<<ans;
    35     return 0;
    36 }
  • 相关阅读:
    Oracle EBS 初始化用户密码
    Oracle EBS FND User Info API
    linux ERROR: ld.so: object '/lib/libcwait.so' from /etc/ld.so.preload cannot be preloaded: ignored.
    linux解压cpio.gz类型文件
    linux删除乱码文件
    linux使用man命令后退出
    linux字符图形界面
    Red Hat linux 如何增加swap空间
    Linux删除文件夹命令
    Linux本地无法登录,远程却可以登录
  • 原文地址:https://www.cnblogs.com/qwerta/p/9670792.html
Copyright © 2011-2022 走看看