P3146 题解&做时思路
作者:互联网
[USACO16OPEN]248 G
题目描述
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 \(N\) positive integers (\(2 \leq N\leq 248\)), each in the range \(1 \ldots 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 \(N\), and the next \(N\) lines give the sequence
of \(N\) numbers at the start of the game.
输出格式
Please output the largest integer Bessie can generate.
样例 #1
样例输入 #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}\) 表示 l 到 r 能合成的最大值。
我们找到一个 \(k\in [l,r)\) 能够使 \(dp_{l,k}=dp_{k+1,r}\) 这就保证了两段都能够取到同一个值,然后直接拼接起来就 ok 了。
题解
做时思路基本正确,只是能够转移还有条件是 \(dp_{l,k}\not=0 \And dp_{k+1,r}\not=0\)
代码
#include <bits/stdc++.h>
#define debug puts("I love Newhanser forever!!!!!");
#define pb push_back
using namespace std;
template <typename T>inline void read(T& t){
t=0; register char ch=getchar(); register int fflag=1;
while(!('0'<=ch&&ch<='9')){if(ch=='-') fflag=-1;ch=getchar();}
while(('0'<=ch&&ch<='9')){t=t*10+ch-'0'; ch=getchar();} t*=fflag;
}
template <typename T,typename... Args> inline void read(T& t, Args&... args){read(t);read(args...);}
const int MAXN=800;
int n,a[MAXN],dp[MAXN][MAXN],ans;
int main(){
read(n);
for(int i=1;i<=n;++i) read(a[i]),dp[i][i]=a[i];
for(int len=1;len<n;++len){
for(int l=1;l<=n-len;++l){
int r=l+len;
for(int k=l;k<r;++k)
if(dp[l][k]==dp[k+1][r]&&dp[l][k]&&dp[k+1][r]) dp[l][r]=dp[l][k]+1,ans=max(ans,dp[l][r]);
}
}
cout<<ans<<endl;
return 0;
}
//Welcome back,Chtholly.
标签:P3146,int,题解,sequence,read,game,she,做时,dp 来源: https://www.cnblogs.com/Mercury-City/p/16417430.html