bzoj 4580: [Usaco2016 Open]248 (dp)
来源:互联网 发布:富贵电玩城源码 编辑:程序博客网 时间:2024/06/07 03:45
4580: [Usaco2016 Open]248
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 99 Solved: 78
[Submit][Status][Discuss]
Description
Bessie likes downloading games to play on her cell phone, even though she does find the small touch
screen rather cumbersome to use with her large hooves.She is particularly intrigued by the current g
ame she is playing. The game starts with a sequence of NN positive integers (2≤N≤248), each in the
range 1…40. In one move, Bessie can take two adjacent numbers with equal values and replace them a
single number of value one greater (e.g., she might replace two adjacent 7s with an 8). The goal is
to maximize the value of the largest number present in the sequence at the end of the game. Please
help Bessie score as highly as possible!
Input
The first line of input contains N, and the next N lines give the sequence of N numbers at the start
of the game.
Output
Please output the largest integer Bessie can generate.
Sample Input
4
1
1
1
2
1
1
1
2
Sample Output
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.
//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.
HINT
Source
Gold
题解:dp
f[i][j][k]表示区间[i,j]变成k是否可行,然后每次得到一个新的数就更新答案。理论复杂度不是很科学,但是有很多的不合法状态可以舍去。
其实可以变成二维的转移,f[i][j]表示区间[i,j]合并成一个数的最大值。那么这样会不会有一个值不这么合并的时候更优呢?因为是枚举长度和区间,所以那样一定是某种状态下的最优值,一定会更新进答案。
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#define N 300using namespace std;int a[N],f[N][N][N],n;int main(){freopen("248.in","r",stdin);freopen("248.out","w",stdout);scanf("%d",&n); int ans=0; int minn=40;for (int i=1;i<=n;i++) scanf("%d",&a[i]),ans=max(ans,a[i]),minn=min(minn,a[i]);memset(f,0,sizeof(f));for (int i=1;i<=n;i++) f[i][i][a[i]]=1;for (int len=2;len<=n;len++) for (int i=1;i<=n;i++) { int pos=i+len-1; if (pos>n) break; int t=ans; for (int col=minn;col<=t;col++) { for (int k=i;k<pos;k++) if (f[i][k][col]&&f[k+1][pos][col]) { f[i][pos][col+1]=1; ans=max(ans,col+1); break; } } }printf("%d\n",ans);}
0 0
- bzoj 4580: [Usaco2016 Open]248 (dp)
- [DP || 贪心 链表] BZOJ 4576 [Usaco2016 Open]262144 & BZOJ 4580 [Usaco2016 Open]248
- BZOJ 4580: [Usaco2016 Open]248
- bzoj 4580: [Usaco2016 Open]248
- [BZOJ4580][Usaco2016 Open]248(dp)
- BZOJ4580: [Usaco2016 Open]248 dp
- 4580: [Usaco2016 Open]248
- bzoj 4581: [Usaco2016 Open]Field Reduction (dfs)
- 【bzoj 4581】[Usaco2016 Open]Field Reduction(dfs)
- BZOJ4580 [Usaco2016 Open]248
- BZOJ4580: [Usaco2016 Open]248
- 【BZOJ4580】[Usaco2016 Open]248【区间DP】【或 贪心】
- bzoj 4586: [Usaco2016 Open]Landscaping 堆
- bzoj 4578 [Usaco2016 OPen]Splitting the Field
- bzoj 4579 [Usaco2016 Open]Closing the Farm
- BZOJ 4582: [Usaco2016 Open]Diamond Collector
- BZOJ 4510: [Usaco2016 Jan]Radio Contact dp
- bzoj 4582: [Usaco2016 Open]Diamond Collector (单调队列+线段树)
- ActiveMQ单机部署zookeeper+levelDB集群
- 业务场景驱动的服务型CMDB
- 13 leetcode - Roman to Integer
- iOS 计步器(系统)
- 用QUERY作为另一个query变量的输入
- bzoj 4580: [Usaco2016 Open]248 (dp)
- Nginx Tomcat负载均衡
- C++11关键字auto如何得到const迭代器
- Unity3D游戏开发之详解 Animation类和Animator类
- 数字三角形
- tuxedo问题描述
- java单例模式(双重检查加锁)
- 带刻度尺的RangeSeekbar
- Phalcon和YII的优点