zoukankan      html  css  js  c++  java
  • hud1003 max num

    一道动态规划  F(x ) = max (F[x-1] + a[x] ,a[x])

    题目:

    Max Sum

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


    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
     
    Author
    Ignatius.L
     
     
    代码:
     
     1 #include <iostream>
     2 using namespace std;
     3 const int maxn = 100000+10;
     4 int arr[maxn];
     5 int sum[maxn];
     6 int main()
     7 {
     8     int tst;
     9     cin>>tst;
    10     int cse= 0;
    11     while(tst--)
    12     {
    13         int n;
    14         cin>>n;
    15         for(int i=0;i<n;i++)
    16         {
    17             cin>>arr[i];
    18         }
    19 
    20         int s=0,e=0,pos=0;
    21         int max = arr[0];
    22         int now  = arr[0];
    23         for(int i=1;i<n;i++)
    24         {
    25             if(now+arr[i]<arr[i])
    26             {
    27                 pos = i;
    28                 now = arr[i];
    29             }
    30             else
    31             {
    32                     now+=arr[i];
    33 
    34             }
    35             if(now > max)
    36             {
    37                 s= pos;
    38                 e = i;
    39 
    40                 max = now;
    41             }
    42         }
    43         cout<<"Case "<<++cse<<":"<<endl;
    44 
    45         cout<<max<<" "<<s+1<<" "<<e+1<<endl;
    46         if(tst)
    47         cout<<endl;
    48 
    49     }
    50 
    51 
    52     return 0;
    53 }
  • 相关阅读:
    java 单点登录机制
    java 使用SAX解析xml 文件
    微信支付demo
    java 23种设计模式 深入理解
    感慨
    GC学习笔记
    mysql5.6乱码
    substring,subsequence,charAt执行效率的不同
    一个数组nums,其中任意两个值等于给定值target,返回这两个值在nums里的位置
    排序时间复杂度对比
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3489629.html
Copyright © 2011-2022 走看看