`
Simone_chou
  • 浏览: 185506 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Super Jumping! Jumping! Jumping!(动态规划)

    博客分类:
  • HDOJ
 
阅读更多

Super Jumping! Jumping! Jumping!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17610    Accepted Submission(s): 7541


Problem Description
Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.



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.
 

 

Input
Input contains multiple test cases. Each test case is described in a line as follow:
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.
 

 

Output
For each case, print the maximum according to rules, and one line one case.
 

 

Sample Input
3 1 3 2
4 1 2 3 4
4 3 3 2 1
0
 

 

Sample Output
4
10
3

    题意:

    跳棋每次都跳到大于该位置值的地方,对这些位置的值进行全部加和,输出最大和。

   

    思路:

    说白了就是求所有递增子序列和的最大值,首先必须要清楚,不是说子序列越长就是和越大,跟之前的最长递增子序列是不一样的。

    对比最长递增子序列与最大和递增子序列:

    最长递增子序列:     1.长度最长  2.递增

    最大和递增子序列:  1.加和最大  2.递增

    可以看出,同样的要求是递增序列。

    设number[1.....N],与sum[1.....N],number表示该位置的值,sum代表每个位置最大和,设i与j两个位置,每次遍历都从 j =1开始到 i 位置结束,故 i 的范围是2到N,j 的范围是1到 i 。

    那么sum[ i ]=number[ i ]+max { sum[1]......sum[i-1] },当然在max { sum[1]......sum[i-1] }这里面的所有项中,要满足条件number[ i ]>number[ j ],因为要求是递增序列。

    与最长递增子序列对比:

    length[ i ]=1+max { length[1]......length[i-1] },同样的,也许要number [ i ]>number [ j ]。

    两者间的区别就是,最大和与本身的值有关,而最长序列与本身值无关,只需要长度+1,这一点在循环体中的写法有点不同。

AC:

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
	int n,i,j;
	int number[1005];
	int sum[1005],max,max1;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0) break;
		memset(number,0,sizeof(number));
		for(i=1;i<=n;i++)
		{
		  scanf("%d",&number[i]);
	          sum[i]=number[i];
		}
//		printf("\n");
//		for(i=1;i<=n;i++)
//		 printf("%d ",number[i]);
//		printf("\n");
//记得初始化
                max=1;
          
		for(i=2;i<=n;i++)
	       {
			max1=0;
                 //这个循环是为了找i前面的最大加和值用的
                        for(j=1;j<i;j++)
			{
			 if(number[i]>number[j])
			   {
			   	 if(sum[j]>max1) max1=sum[j];
			   }
		        }
                 //找到后就加上这个值成为符合条件下的自身最大加和值 
		    sum[i]+=max1;
//对比查找最大用的,直接写在同一个循环内,就不用再另外写一个来找里面的最大值输出了
                       if(sum[i]>max) max=sum[i];
//			printf("\nmax1=%d\n",max1);
//		    printf("sum[%d]=%d\n",i,sum[i]);
//		    printf("max=%d\n",max);
//		    system("pause");
//测试用
		}
		printf("%d\n",max);
	}
	return 0;
}

 Compare:

    最长递增子序列:

for(i=1;i<length;i++)  
    {  
        for(j=0;j<i;j++)  
         if(s[i]>s[j])  
          max[i]=max[i]>max[j]+1?max[i]:max[j]+1;  
//直接对比更新值
        if(max[i]>m) m=max[i];     
    }

   最大和递增子序列:

for(i=2;i<=n;i++)
 {
   max1=0;
   for(j=1;j<i;j++)
   {
    if(number[i]>number[j])
      {
	if(sum[j]>max1) max1=sum[j];
      }
   }
//这里不可以直接对比更新值,必须j循环完才可以
    sum[i]+=max1;
    if(sum[i]>max) max=sum[i];
 }

   总结:

   还是需要提高一下实现能力,不能急,得慢慢来……

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics