ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

P3146 [USACO16OPEN]248 G(区间DP)

2021-10-20 11:31:07  阅读:158  来源: 互联网

标签:NN int she 合并 game DP 248 dp USACO16OPEN


题目描述

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

区间dp典型例题。注意如果区间不能完全合并也要标记,否则会T飞。

#include <bits/stdc++.h>
using namespace std;
int n, a[105], dp[405][405];//dp[i][j]表示第i个到第j个这一段全部合并能够得到的最大值
int f(int l, int r) {
	if(dp[l][r] != 0) return dp[l][r];
	for(int i = l; i < r; i++) {
		if(f(l, i) == -1 || f(i + 1, r) == -1) continue;//如果当前段不能全部合并 也要标记
		dp[l][r] = max(dp[l][r], (f(l, i) == f(i + 1, r) ? f(l, i) + 1 : 0));
	}
	if(dp[l][r] == 0) dp[l][r] = -1;
	return dp[l][r];
}
int main() {
	cin >> n;
	memset(dp, 0, sizeof(dp));
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		dp[i][i] = a[i];
	}
	int ans = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			ans = max(ans, f(i, j));
		}
	}
	cout << ans << endl;
	return 0;
}

标签:NN,int,she,合并,game,DP,248,dp,USACO16OPEN
来源: https://www.cnblogs.com/lipoicyclic/p/15428164.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有