最大连续子序列
来源:互联网 发布:java poijar包 编辑:程序博客网 时间:2024/05/21 15:50
老题了,就是多了一个子数列的首尾元素,dp[i]=max(dp[i-1]+a[i],a[i]);需要改一下
if(dp[i-1]<0)dp[i]=a[i];x=y=i;
if(dp[i-1]>=0)dp[i]=dp[i-1]+a[i];y=i;
最后遍历注意一定是大于,否则j有可能不是最小的。
#include<cstdio>#include<cstring>#include<iostream>using namespace std;int a[100005];struct Dp{int ans;int x;int y;}dp[100005];int max(int a, int b){return a > b ? a : b;}int main(){int i, j, m, n, k, ans;Dp temp;while (scanf("%d", &k) != EOF){if (!k)break;for (i = 1; i <= k; i++)scanf("%d", &a[i]);dp[1].ans = a[1]; dp[1].x = 1; dp[1].y = 1;for (i = 2; i <= k; i++){if (a[i] > dp[i - 1].ans + a[i]){dp[i].ans = a[i]; dp[i].x = i; dp[i].y = i;}else{dp[i].ans = dp[i - 1].ans + a[i];dp[i].x = dp[i - 1].x; dp[i].y = i;}}temp = dp[1];for (i = 2; i <= k; i++){if (dp[i].ans > temp.ans)temp = dp[i];}if (temp.ans > 0)cout << temp.ans << " " << a[temp.x] << " " << a[temp.y] << endl;else if (temp.ans == 0)cout << "0 0 0" << endl;else if (temp.ans < 0)cout << "0 " << a[1] << " " << a[k] << endl;}return 0;}
0 0
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- 最大连续子序列
- LeetCode—Remove Linked List Elements—C++
- php的事件处理机制(回调函数)
- conn 配置
- JSON再研究
- Oracle INSERT 语句
- 最大连续子序列
- virsh console
- Longest Substring Without Repeating Character
- LSM树由来、设计思想以及应用到HBase的索引
- 2015061703 - mysql数据类型整理
- 第009讲 初识css 类选择器 id选择器 html选择器
- 如何敲代码
- CloseHandle()函数的使用
- DAO设计模式