HDU1003 dp 动态规划解析

 

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.


#include<iostream>
using namespace std;
int main()
{
 int N;
 cin>>N;
 int i,count=1;
 while(N--)
 {
  int n;
  int now,before,sum,x,y,z;
  cin>>n;
  for(i=1;i<=n;i++)
  {
   cin>>now;
   if(i==1)
   {
    sum=before=now;//sum保存最大和,before保存当前的和 ,x表示before的起始位置,y,z表示sum的始末位置
    x=y=z=i;
   }
   else
   {
    if(now+before>now)
     before=before+now;
    else
    {
     befor=now;
     x=i;//预存位置要重置
    }
   }
   if(before>sum)
   {
    sum=before;
    y=x;
    z=i;
   }

  }
  cout<<"Case "<<count++<<":"<<endl<<sum<<" "<<y<<" "<<z<<endl;
  if(N>=1)
   cout<<endl;
 }
 return 0;
}

posted @ 2016-03-28 15:30  左岸繁华右岸殇  阅读(294)  评论(0编辑  收藏  举报