Super Jumping! Jumping! Jumping!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 43519 Accepted Submission(s): 20137

The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.
3 1 3 2 4 1 2 3 4 4 3 3 2 1 0
4 10 3记忆化搜索:#include<bits/stdc++.h> int n,a[1002],dp[1002]; using namespace std; int dfs(int x) { if(dp[x]) return dp[x];//若此前已得出了从x点出发所能得到的最大值则返回该值 int ma=a[x],i,j; for(i=x+1;i<=n;i++) { if(a[i]>a[x]) { ma=max(a[x]+dfs(i),ma); } } return dp[x]=ma; } int main() { int i,j; while(scanf("%d",&n)!=EOF) { if(!n)break; memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) scanf("%d",&a[i]); int ma=-1; for(i=n;i>=1;i--)//因为只能从前向后走,所以循环从最后开始,优化时间复杂度 { int t=dfs(i); ma=max(ma,t); } printf("%d\n",ma); } return 0; }动态规划:#include<bits/stdc++.h> int n,a[1002],dp[1002]; using namespace std; int main() { int i,j; while(scanf("%d",&n)!=EOF) { if(!n)break; for(i=1;i<=n;i++) scanf("%d",&a[i]); int ma=a[1]; dp[1]=a[1]; for(i=2;i<=n;i++) { dp[i]=a[i];//若直接跳到i点,dp[i]=a[i]; for(j=i-1;j>=1;j--)//此循环遍历从1至i-1跳到i的情况,找出最优解 { if(a[i]>a[j]) dp[i]=max(dp[i],dp[j]+a[i]); } ma=max(ma,dp[i]); } printf("%d\n",ma); } return 0; }
253




被折叠的 条评论
为什么被折叠?



