zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    0593. Valid Square (M)
    0832. Flipping an Image (E)
    1026. Maximum Difference Between Node and Ancestor (M)
    0563. Binary Tree Tilt (E)
    0445. Add Two Numbers II (M)
    1283. Find the Smallest Divisor Given a Threshold (M)
    C Primer Plus note9
    C Primer Plus note8
    C Primer Plus note7
    C Primer Plus note6
  • 原文地址:https://www.cnblogs.com/yfz1552800131/p/5329226.html
Copyright © 2011-2022 走看看