zoukankan      html  css  js  c++  java
  • 洛谷p3146&p3147

    248

    题目描述

    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 NN positive integers (2 \leq N\leq 2482N248), each in the range 1 \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 NN, and the next NN lines give the sequence

    of NN 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.

    262144:

    题目描述

    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 NN positive integers (2 \leq N\leq 262,1442N262,144), each in the range 1 \ldots 40140. In one move, Bessiecan take two adjacent numbers with equal values and replace them asingle number of value one greater (e.g., she might replace twoadjacent 7s with an 8). The goal is to maximize the value of thelargest number present in the sequence at the end of the game. Pleasehelp Bessie score as highly as possible!

    Bessie喜欢在手机上下游戏玩(……),然而她蹄子太大,很难在小小的手机屏幕上面操作。

    她被她最近玩的一款游戏迷住了,游戏一开始有n个正整数,(2<=n<=262144),范围在1-40。在一步中,贝西可以选相邻的两个相同的数,然后合并成一个比原来的大一的数(例如两个7合并成一个8),目标是使得最大的数最大,请帮助Bessie来求最大值。

    输入格式

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

    of NN 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.

    ——————————————————————————分割线————————————————————————————

    这两个题目乍一看,好像是一样的(好吧其实的确是差不多),在一乍才发现数据不一样,一个是0——248,一个是1——262144

    题目名字也就是这么来的,那么很明显,第一道题目可以套三重循环,没有超时

    第一道题代码(这个应该不用多说吧......)

    #include<bits/stdc++.h>
    using namespace std;
    
    int number,num[302],dp[302][302],ans=-1e9;
    
    int read(){
        int s=0,w=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
        while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
        return s*w;
    }
    
    int main(){
        number=read();
        for(int i=1;i<=number;i++)num[i]=read(),dp[i][i]=num[i];
        for(int i=number-1;i>=1;i--)
           for(int j=i+1;j<=number;j++)
              for(int k=i;k<j;k++){
                  if(dp[i][k]==dp[k+1][j])dp[i][j]=max(dp[i][j],dp[i][k]+1),ans=max(ans,dp[i][j]);
              }
        cout<<ans;
        return 0;
    }

    好的,再是第二题

    如果还用第一题的方法262144^3肯定超过了100000000,超时了,并且dp[262144][262144]也超空间了

    那么就要用一种新方法(虽然还是DP)

    定义:dp[i][j]表示以j为左端点,合成i的右端点,看着这么麻烦,用简洁的语言表示就是一个,左端点为j,右端点为dp[i][j],合成i的区间

    那么可以得出动态转移方程dp[i][j]=dp[i-1][dp[i-1][j]]

    可能比较难懂,用文字表达一下,dp[i-1][j]表示能够合成i-1的右端点,那么dp[i-1][dp[i-1][j]]是以一个合成i-1的区间的右端点为左端点的右端点

    好吧,有点拗口,用图片说明

    手推可以求出,以3为左端点能够合成3的区间就是[3,4],所以说dp[i-1][j]=4,那么以4为左端点,能够合成3的区间为[4,6],而两个值为3的区间合并以后不就是i吗

    那么合成i的区间的右端点就是以4为左端点合成i-1的右端点,左端点就是j,所以右端点表达式就是这样了

    上代码:

    //dp
    #include<bits/stdc++.h>
    using namespace std;
    
    int number,num[262145],dp[60][262145],ans=0;
    
    int read(){
        int s=0,w=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
        while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
        return s*w;
    }
    
    int main(){
        number=read();
        for(int i=1;i<=number;i++)num[i]=read(),dp[num[i]][i]=i+1;
        for(int i=2;i<=58;i++)
           for(int j=1;j<=number;j++){
               if(!dp[i][j])dp[i][j]=dp[i-1][dp[i-1][j]];
               if(dp[i][j])ans=i;
           }
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    关于 Xcode8打印JSON的时候,NSLog控制台显示不完整
    关于Xcode8打印一堆log问题
    iOS---关于UIWebView
    iOS --- UIWebView的加载本地数据的三种方式
    关于iOS10
    iOS切图文件的命名规范
    iOS---A valid provisioning profile for this executable was not found
    iOS---用Application Loader 上传的时候报错No suitable application records were found. Verify your bundle identifier 'xx' is correct
    2020Python作业15——装饰器2+迭代器
    【2020Python修炼记21】Python语法入门—生成器
  • 原文地址:https://www.cnblogs.com/GMSD/p/11263627.html
Copyright © 2011-2022 走看看