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;
    }

  • 相关阅读:
    Beef McNuggets USACO 4.1(数论公约数问题+背包阵亡)
    Raucous Rockers USACO 3.4 (dp背包?)
    Electric Fence USACO 3.4
    大雾....
    American Heritage USACO 3.4 (二叉树前序中序求后序)
    Closed Fences USACO 3.4(阵亡)
    cvte酱油一把
    算法导论之计算几何学小记 33.1
    A Game USACO 3.3 (DP阵亡)
    [bx]和loop指令
  • 原文地址:https://www.cnblogs.com/yfz1552800131/p/5329226.html
Copyright © 2011-2022 走看看