• hdu-1003 Max Sum (最大子段和)


    http://acm.hdu.edu.cn/showproblem.php?pid=1003

    Max Sum

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 202859    Accepted Submission(s): 47418

    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

    题意:求一个数列的最大子序列和

    简单的动态规划,用dp[i]来记录i为终点的序列的最大子段和

    如果dp[i-1]>0  dp[i] = dp[i-1] + a[i]

    否则dp[i] = a[i]

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<string>
     6 #include<cmath>
     7 using namespace std;
     8 int a[110000], dp[110000];
     9 int main(){
    10     int T, n, i, j, x, y, cas, xx, yy;
    11     cin>>T;
    12     for(cas=1; cas<=T; cas++){
    13         cin>>n;
    14         for(i=1; i<=n; i++)
    15             cin>>a[i];
    16         int mmax = -100000;
    17         memset(dp, 0, sizeof(dp));
    18         dp[1] = a[1];
    19         x = 1, y = 1;
    20         xx = 1, yy = 1;
    21         for(i=1; i<=n; i++){
    22             if(dp[i-1]>=0){
    23                 dp[i] = dp[i-1] + a[i];
    24                 yy = i;
    25             }
    26             else{
    27                 dp[i] = a[i];
    28                 xx = i;
    29                 yy = i;
    30             }
    31             if(mmax <= dp[i]){
    32                 mmax = dp[i];
    33                 y = yy;
    34                 x = xx;
    35             }
    36         }
    37 //        for(i=1; i<=n; i++)
    38 //            cout<<dp[i]<<" ";
    39 //        cout<<endl;
    40         printf("Case %d:
    ",cas);
    41         printf("%d %d %d
    ",mmax,x,y);
    42         if(cas!=T)
    43             cout<<endl;
    44     }
    45     return 0;
    46 }
     

    还可以穷举

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 using namespace std;
     7 int a[110000];
     8 int main(){
     9     int T, n, i, j, sum, k, ans, cas, x, y;
    10     cin>>T;
    11     for(cas=1; cas<=T; cas++){
    12         cin>>n;
    13         for(i=1; i<=n; i++)
    14             cin>>a[i];
    15         sum = 0;
    16         ans = -1000000;
    17         k = 1;
    18         for(i=1; i<=n; i++){
    19             sum += a[i];
    20             if(sum>ans){
    21                 ans = sum;
    22                 x = k;
    23                 y = i;
    24             }
    25             if(sum<0){
    26                 sum = 0;
    27                 k = i+1;
    28             }
    29         }
    30         printf("Case %d:
    ",cas);
    31         printf("%d %d %d
    ",ans, x, y);
    32         //cout<<cas<<" "<<T<<endl;
    33         if(cas!=T)
    34             printf("
    ");
    35     }
    36     return 0;
    37 }

    这里的sum就相当于dp[i]

  • 相关阅读:
    数学趣题——数字的全排列
    数学趣题——谁在说谎
    数学趣题——移数字游戏
    数学趣题——魔幻方阵
    数学趣题——亲密数
    数学趣题——数字翻译器
    数学趣题——求圆周率的近似值
    数学趣题——完全数
    Linux:CentOS 7 连接ssh方法
    Linux:CentOS 7 服务器之间ssh无密码登录、传输文件
  • 原文地址:https://www.cnblogs.com/wudi-accept/p/5330080.html
Copyright © 2020-2023  润新知