zoukankan      html  css  js  c++  java
  • 连续子数组的最大和

       1:  /*
       2:  @@
       3:      Author:    Justinzhang
       4:      Email:    uestczhangchao@gmail.com
       5:      time:    2012-9-1 21:40:13
       6:      desc:    find the max sum of sub arrys; This file was created at an earlier time; 
       7:              At that time, i didn't read the programming perls, in that book, there is a detailed
       8:              discussion about this problem. The fastest algrithm can solve it in O(n) time;
       9:              So if have spare time, read more of it.
      10:  @@
      11:  */
      12:   
      13:  #include <iostream>
      14:  #include <vector>
      15:  #include <algorithm>
      16:  #include <iomanip>
      17:  using namespace std;
      18:   
      19:  template<class type> 
      20:  type getMax(type param1, type param2)
      21:  {
      22:      return (param1>param2 ? param1 : param2);
      23:  }
      24:   
      25:  template<class type> type find_max_sum_of_subarray(vector<type> vec)
      26:  {
      27:      type maxSum = 0;
      28:      type curMaxSum = 0;
      29:      for(unsigned int i=0; i < vec.size(); i++)
      30:      {
      31:          curMaxSum += vec[i];
      32:          curMaxSum = getMax(0, curMaxSum);
      33:          maxSum = getMax(maxSum, curMaxSum);
      34:      }
      35:      return maxSum;
      36:  }
      37:   
      38:   
      39:  int main()
      40:  {
      41:      int array[] = {-2, 5, 3,-6, 44, -8, 16};
      42:      /* /!\ note that: to use array to initialize vector, the second parameter is the length of array,
      43:         not the length of array minus 1.
      44:      */
      45:      vector<int> vec(array,array+(sizeof array / sizeof(int)));
      46:      cout << "find_max_sum_of_subArray: " << find_max_sum_of_subarray<int>(vec) << endl;
      47:      return 0;
      48:  }
  • 相关阅读:
    HDU-2222 Keywords Search(AC自动机)
    HDU-2647 Reward(拓扑排序)
    HDU-2896 病毒侵袭(AC自动机)
    UESTC-1057 秋实大哥与花(线段树+成段加减+区间求和)
    CSU-1120 病毒(最长递增公共子序列)
    记忆化搜索
    区间动态规划 矩阵连乘 Medium
    34枚金币时间管理法
    摄影基础1
    学习法则 讲
  • 原文地址:https://www.cnblogs.com/justinzhang/p/2667178.html
Copyright © 2011-2022 走看看