当前位置: 代码迷 >> 综合 >> 【P3146】 【USACO16OPEN】248
  详细解决方案

【P3146】 【USACO16OPEN】248

热度:47   发布时间:2024-01-19 22:31:18.0

题目描述:

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 2482≤N≤248), each in the range 1 \ldots 401…40. 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.

首先,奇妙的是:为什么dp的问题除了背包都这么难?

下面进入正题: 该题的转移方程为:
dp[l][r]=max(dp[l][r],dp[l][k]+1)[ dp[l][k]==dp[k+1][r]]dp[l][r]=max(dp[l][r],dp[l][k]+1)[dp[l][k]==dp[k+1][r]]
直接上代码,具体的问题下面有:

代码:

#include<iostream>
#include<cstdio>
#include<cctype>
using namespace std;
#define maxn 257
int n,dp[maxn][maxn],a[maxn],ans;
inline int read()
{int num=0;char c,sf=1;while(isspace(c=getchar()));if(c=='-') sf=-1,c=getchar();while(num=num*10+c-48,isdigit(c=getchar()));return num*sf;
}
int main()
{n=read();//inputfor(int i=1;i<=n;i++) {dp[i][i]=read();ans=max(dp[i][i],ans);//在没有开始合并之前,最小值为单个数值}for(int len=2;len<=n;len++)for(int l=1;l+len-1<=n;l++){int r=l+len-1;//找到另一边for(int k=l;k<r;k++)//找到中点if(dp[l][k]==dp[k+1][r]) dp[l][r]=max(dp[l][r],dp[l][k]+1);//如果这两个区域的总和数值相同,那么看数值大小并合并与把数值+1ans=max(ans,dp[l][r]);//算出最大值}printf("%d",ans);return 0;
}