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

  • 相关阅读:
    curl 命令行使用参考
    PHP 输出json_encode 空白的检查
    RAM和ROM
    浮点数
    负数补码
    位运算
    无法加载文件 C:UsershuangshiminAppDataRoaming pmwechat-terminal.ps1,因为在此系统上禁止运行脚本
    windows + php + shell_exec 执行失败的可能原因
    Ubuntu 发送邮件
    红黑树
  • 原文地址:https://www.cnblogs.com/yfz1552800131/p/5329226.html
Copyright © 2011-2022 走看看