工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1535|回复: 2

[ACM] 最大子段和

[复制链接]
发表于 2007-1-23 18:13 | 显示全部楼层 |阅读模式
题目:
http://acm.hdu.edu.cn/showproblem.php?pid=1003
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3178   Accepted Submission(s) : 564



Problem Description


Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.



Input


The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).



Output


For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.



Sample Input


2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output


Case 1:
14 1 4

Case 2:
7 1 6


算法暂时就不描述了
代码很长,希望能帮忙找找BUG...
总是得到WA  
(提交时 getch() 去掉了的.)
  1. #include "stdio.h"
  2. int main()
  3. {
  4.     long int max,in,a[3],n=0;
  5.     int i=0,j=0,p,s,e,len;
  6.     int as,ae,bs,be;
  7. scanf("%ld",&n);
  8. while(n--){
  9.     a[0]=a[1]=a[2]=-1001;
  10.     j++;
  11.     scanf("%d",&len);
  12.     if(len>0){
  13.         scanf("%ld",&in);
  14.         max=in;
  15.         p=1;
  16.         ae=be=as=bs=s=e=1;
  17.         i=0;
  18.         /*
  19.          *处理开始处的零与负数
  20.          */
  21.         while(in<0 && p<len){

  22.             if(max<in){
  23.                 max=in;
  24.                 s=e=p;
  25.             }
  26.             scanf("%ld",&in);
  27.             bs++;
  28.             p++;
  29.         }
  30.         if(p>len){goto output;}/*___BAD___BAD___*/

  31.         a[0]=in;
  32.         as=ae=p;
  33.         if(p>=len)goto output;
  34.         scanf("%ld",&in);
  35.         p++;
  36.         while(in>=0 && p<=len){
  37.             a[0]+=in;
  38.             ae=p;
  39.             if(p>=len)goto output;
  40.             scanf("%ld",&in);
  41.             p++;
  42.         }
  43.         if(a[0]>max){
  44.             max=a[0];
  45.             s=as;e=ae;
  46.         }
  47.         a[1]=in;i=1;




  48.         while(1){
  49.             if(p>=len)goto output;
  50.             scanf("%ld",&in);
  51.             p++;
  52.             switch(i){
  53.                 case 1:
  54.                     if(in<0){
  55.                         a[1]+=in;   
  56.                     }
  57.                     else{
  58.                         a[2]=in;i=2;bs=be=p;/*调整a[2]的位置*/
  59.                     }break;
  60.                 case 2:
  61.                     if(in>=0){
  62.                         a[2]+=in;
  63.                         be=p;
  64.                     }
  65.                     else{
  66.                         
  67.                         /*调整max*/
  68.                         if(max<a[0]){
  69.                             max=a[0];s=as;e=ae;
  70.                         }
  71.                         if(max<a[0]+a[1]+a[2]){
  72.                             max=a[0]+a[1]+a[2];
  73.                             s=as;e=be;
  74.                         }
  75.                         if(max<a[2]){
  76.                             max=a[2];
  77.                             s=bs;e=be;
  78.                         }

  79.                         /*调整下一个a[0]*/
  80.                         if(a[0]+a[1]>0){
  81.                             a[0]=a[0]+a[1]+a[2];
  82.                             ae=be;/*下一个a[0]*/
  83.                         }else{
  84.                             a[0]=a[2];
  85.                             as=bs;ae=be;
  86.                         }
  87.                         i=1;
  88.                         a[1]=in;
  89.                     }break;
  90.             }/*End of switch*/
  91.         }
  92.     }/*End of one case's deal.*/


  93. output:
  94.     if(max<a[0]){
  95.         max=a[0];
  96.         e=ae;s=as;
  97.     }
  98.     if(max<a[0]+a[1]+a[2]){
  99.         max=a[0]+a[1]+a[2];
  100.         s=as;e=be;
  101.     }
  102.     if(max<a[2]){
  103.         max=a[2];
  104.         s=bs;e=be;
  105.     }

  106.     printf("Case %d:\n%ld %d %d\n",j,max,s,e);
  107.     if(n!=0)printf("\n");
  108. }/*End of n's loop*/
  109. getch();

  110. return 0;
  111. }
复制代码

[ 本帖最后由 找不了回去的路 于 2007-1-23 18:15 编辑 ]
发表于 2007-2-8 10:52 | 显示全部楼层
其实楼上可以用动态规划解
回复

使用道具 举报

发表于 2007-2-8 17:31 | 显示全部楼层
其实就是动态规划...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2024-6-13 00:11

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表