Chinaunix首页 | 论坛 | 博客
  • 博客访问: 127970
  • 博文数量: 11
  • 博客积分: 194
  • 博客等级: 入伍新兵
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-24 08:20
文章分类

全部博文(11)

文章存档

2013年(8)

2012年(3)

分类: C/C++

2012-11-10 14:22:52


Description

Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N). 


You should output S. 

Input


The input will consist of several test cases. For each test case, one integer N (2 <= N <= 100000) is given in the first line. Second line contains N integers. The input is terminated by a single line with N = 0.


Output


For each test of the input, print a line containing S.

Sample Input


-5 9 -5 11 20 
0


Sample Output


40



额, 算法弱菜写这些真不容易
一道很简单的动态规划,枚举出所有情况前后各部分最大子串的和,取最大值即可

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <climits>

  4. int main()
  5. {
  6.     int m, i, s, max, a[100001], pre[100001];
  7.     
  8.     while (std::cin >> m, m) {
  9.         s = 0;
  10.         max = INT_MIN;
  11.         for (i = 0; i != m; ++i) {
  12.             scanf("%d", &a[i]);
  13.             s += a[i];
  14.             if (s > max) {
  15.                 max = s;
  16.             }
  17.             pre[i] = max;
  18.             if (s < 0) {
  19.                 s = 0;
  20.             }
  21.         }
  22.         s = 0;
  23.         max = INT_MIN;
  24.         for (i = m - 1; i != 0; --i) {
  25.             s += a[i];
  26.             if (s + pre[i - 1] > max) {
  27.                 max = s + pre[i - 1];
  28.             }
  29.             if (s < 0) {
  30.                 s = 0;
  31.             }
  32.         }
  33.         std::cout << max << std::endl;
  34.     }

  35.     return 0;
  36. }

阅读(1234) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:ubuntu下显卡驱动问题

给主人留下些什么吧!~~