• HDU 1003


    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

    大致题意:

      给一个序列,找出他的和最大的子串;

    解题思路:

      以下有两个代码:

      第一个代码:

        贪心,遍历所有可能,找最大,当然会超时,所以要“剪枝”;复杂度O(n^2);

      第二个代码:

         比较神奇,DP,复杂度0(n);

        以a[0]结尾的子序列只有a[0]
        以a[1]结尾的子序列有 a[0]a[1]和a[1]
        以a[2]结尾的子序列有 a[0]a[1]a[2] / a[1]a[2] / a[2]

        很容易得出状态转移方程式 sum_a[i]=max(sum_a[i-1]+a[i],a[i]);

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 int main()
     5 {
     6     int t,n,a[100005];
     7     cin>>t;
     8     for(int k=1;k<=t;k++)
     9     {
    10         cin>>n;
    11         for(int i=1;i<=n;i++)
    12             cin>>a[i];
    13         int max,ans=-99999999,a1,a2;
    14         for(int i=1;i<=n;i++)
    15         {
    16             max=0;
    17             for(int j=i;j<=n;j++)
    18             {
    19                 max+=a[j];
    20                 if(max>ans)
    21                 {
    22                    ans=max;
    23                    a1=i;
    24                    a2=j;
    25                 }
    26                 if(max<0) break;//显然跳过处理
    27             }
    28         }
    29         printf("Case %d:
    ",k);
    30         printf("%d %d %d
    ",ans,a1,a2);
    31         if(k<t) cout<<endl;
    32     }
    33 }
     1 #include <cstdio>
     2 int main(){
     3     int t,cas=0;
     4     scanf("%d",&t);
     5     while(t--&&++cas)
     6     {
     7         if(cas!=1) printf("
    ");
     8         printf("Case %d:
    ",cas);
     9         int a[100001],f[100001]={0};
    10         int max,maxl,maxr,l,r,n;
    11         scanf("%d",&n);
    12         l=1; max=-100000;
    13         for(int i=1;i<=n;i++){
    14             scanf("%d",&a[i]);
    15             if(f[i-1]+a[i]<a[i]) { f[i]=a[i]; l=i; r=i; }
    16             else{ f[i]=f[i-1]+a[i]; r=i; }
    17             if(max<f[i]){ max=f[i]; maxl=l; maxr=r; }
    18         } printf("%d %d %d
    ",max,maxl,maxr);
    19     } return 0;
    20 }
    我自倾杯,君且随意
  • 相关阅读:
    pch”预编译头文件来自编译器的其他版本,或者预编译头为 C++ 而在 C 中使用它(或相反) and vs找不到路径
    VC++获得当前系统时间的几种方案
    无法解析的外部符号 __imp____glutInitWithExit@12,该符号在函数 _glutInit_ATEXIT_HACK@8 中被引用
    error C2381: “exit” : 重定义
    vs2019 link glaux.lib 无法解析的外部符号,是缺少一个Lib
    操作系统算法整理+汇总
    vs2019配置glm包
    第二题 既约分数
    死磕Spring之IoC篇
    精尽Spring Boot源码分析
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/5159183.html
Copyright © 2020-2023  润新知