POJ 2479 解题报告
来源:互联网 发布:number是什么软件 编辑:程序博客网 时间:2024/06/03 20:06
这道题是对求一个数组中最大连续子数组和的改编,求两段最大子数组和的和。Leetcode上面好多类似的。看了discuss才回想起来。代码重复的地方很多。其实就是最大子数组和的代码,左右各求一遍。这样对任意位置,都知道左边和右边的最大和是多少,加起来就可以。
2479Accepted720K438MSC++1635B/* ID: thestor1 LANG: C++ TASK: poj2479 */#include <iostream>#include <fstream>#include <cmath>#include <cstdio>#include <cstring>#include <limits>#include <string>#include <vector>#include <list>#include <set>#include <map>#include <queue>#include <stack>#include <algorithm>#include <cassert>using namespace std;const int MAXN = 50000;int main(){int nums[MAXN];int leftsums[MAXN], rightsums[MAXN];int T;scanf("%d", &T);for (int t = 0; t < T; ++t){int n;scanf("%d", &n);for (int i = 0; i < n; ++i){scanf("%d", &nums[i]);}bool allneg = true;int maxneg = INT_MIN;int sum = 0, maxsum = INT_MIN;for (int i = 0; i < n; ++i){if (allneg && nums[i] < 0){maxneg = max(maxneg, nums[i]);leftsums[i] = maxneg;}else{allneg = false;sum += nums[i];if (sum < 0){sum = 0;}maxsum = max(maxsum, sum);leftsums[i] = maxsum;}}allneg = true;maxneg = INT_MIN;sum = 0, maxsum = INT_MIN;for (int i = n - 1; i >= 0; --i){if (allneg && nums[i] < 0){maxneg = max(maxneg, nums[i]);rightsums[i] = maxneg;}else{allneg = false;sum += nums[i];if (sum < 0){sum = 0;}maxsum = max(maxsum, sum);rightsums[i] = maxsum;}}maxsum = INT_MIN;for (int i = 0; i < n - 1; ++i){if (leftsums[i] + rightsums[i + 1] > maxsum){maxsum = leftsums[i] + rightsums[i + 1];}}printf("%d\n", maxsum);}return 0; }
0 0
- poj 2479解题报告
- POJ 2479 解题报告
- poj 2593 & poj 2479解题报告
- POJ - 2479 Maximum sum解题报告
- POJ 2479 Maximum sum 解题报告
- poj数百篇解题报告
- poj 1141解题报告
- poj 1001解题报告
- poj 1011 解题报告
- poj 1087 解题报告
- poj 2153解题报告
- poj 2051解题报告
- POJ 1167 解题报告
- poj 1026 解题报告
- POJ 1077 解题报告
- POJ 1042 解题报告
- POJ 1095 解题报告
- POJ 1159解题报告
- 架构师害怕程序员知道的十项技能
- Oracle在所有内容前追加一些内容的方法
- Visual Studio 2013常用快捷键
- 3D printing of the Obama
- 【随时更新】http和php相关知识点积累
- POJ 2479 解题报告
- 在Eclipse中运行hadoop程序
- 自适应肤色识别
- DFS非递归实现图的遍历
- WdatePicker 没有权限(IE下报错)
- python yield解读
- Android RadioGroup控件的使用
- 第16周项目3-用函数指针调用函数
- 单表查询如何建立索引让你的查询语句性能达到最高