大家好,又见面了,我是你们的朋友全栈君。
题目描述:
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 game she is playing. The game starts with a sequence of N positive integers (2≤N≤262,144), 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!
给定n个1到40之间的数,每次可以将两个相邻的相等的数替换成一个比它们大1的数(比如,相邻的7 7可以替换成8),你需要让最后剩下的数中的最大数尽量大
输入:
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.
算法标签:DP(很常见的一个类似倍增的dp)
思路:
其实合并的过程很类似一个倍增的过程,且仔细分析发现数字的大小最大值58大概,所以就用
当f[i][j]!=0&&f[f[i][j]+1]!=0时可以有转移f[i][j+1]=f[f[i][j]+1][j];
以下代码:
#include<bits/stdc++.h> #define il inline #define _(d) while(d(isdigit(ch=getchar()))) using namespace std; const int N=262150;int n,f[N][70],m; il int read(){ int x,f=1;char ch;_(!)ch=='-'?f=-1:f;x=ch^48;_()x=(x<<1)+(x<<3)+(ch^48);return f*x;} int main() { n=read();for(int i=1;i<=n;i++){ int x=read();f[i][x]=i;m=max(m,x);} for(int i=1;i<=m;i++)for(int j=1;j<=n;j++) if(f[j][i]!=0&&f[f[j][i]+1][i]!=0){ f[j][i+1]=f[f[j][i]+1][i];if(i+1>m)m=i+1; }printf("%d\n",m); return 0; }
View Code
转载于:https://www.cnblogs.com/Jessie-/p/9845740.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/107236.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...