1007. Maximum Subsequence Sum (25)
来源:互联网 发布:camera软件下载 编辑:程序博客网 时间:2024/06/09 19:46
1007. Maximum Subsequence Sum (25)
目录
- Maximum Subsequence Sum 25
- 目录
- 原题
- 思路
- 代码
原题
Given a sequence of K integers
N1,N2,...,NK . A continuous subsequence is defined to beNi,Ni+1,...,Nj where1<=i<=j<=K . The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
思路
用动态规划解决此问题。
1. 设置数组dp,dp[i]代表以第i个元素结尾的最大子序列和。设置数组start,start[i]代表以第i个元素结尾的最大子序列的起始位置。设置int max = INT_MIN, max_index = k - 1。设置flag=true,序列中的元素是否全部为零。2. 读入k。3. 读入第一个数字array[0]。对于第一个数字,dp[i] = array[0],start[i] = 0。如果array[0]>=0,那么max = array[0], max_index = 0,flag=false。4. 循环k-1次。 1. 读入第i个数字array[i]。 2. 如果array[i]大于等于零,flag=false。 3. 如果dp[i-1] < 0,start[i] = i, dp[i] = temp。 4. 否则,start[i] = start[i-1],dp[i]=dp[i-1]+temp。 5. 如果dp[i] > max,那么max = dp[i], max_index = i。5. 输出max,如果flag == true,输出array[0],array[k-1],否则输出array[start[max_index]],array[max_index]。
代码
#include <iostream>#include <cstdio>#include <climits>int main(void){ setvbuf(stdin, new char[1 << 20], _IOFBF, 1 << 20); setvbuf(stdout, new char[1 << 20], _IOFBF, 1 << 20); int k; scanf("%d", &k); int *dp = new int[k], *start = new int[k], max = INT_MIN, max_index = k - 1; int *array = new int[k]; scanf("%d", array); bool flag = true; if (array[0] >= 0) { max = array[0]; max_index = 0; flag = false; } dp[0] = array[0]; start[0] = 0; for (int i = 1; i < k; i++) { scanf("%d", array + i); if (array[i] >= 0) { flag = false; } if (dp[i - 1] < 0) { dp[i] = array[i]; start[i] = i; } else { dp[i] = dp[i - 1] + array[i]; start[i] = start[i - 1]; } if (max < dp[i]) { max = dp[i]; max_index = i; } } if (flag) { printf("%d %d %d\n", 0, array[0], array[k - 1]); } else { printf("%d %d %d\n", max, array[start[max_index]], array[max_index]); } delete[] array; delete[] start; return 0;}
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- Sofia-SIP辅助文档九 - Sofia SIP用户代理库 - "ipt" - 工具模块
- android jni中的java调c的两种方法
- Android项目使用python分语言批量打包apk渠道包
- Sofia-SIP辅助文档十 - Sofia SIP用户代理库 - "nua" - 高层用户代理模块
- Python学习
- 1007. Maximum Subsequence Sum (25)
- http请求资源下载还是打开显示
- 给VMWare虚拟机瘦身
- Sofia-SIP辅助文档十一 - Sofia SIP用户代理库 - "nea" - SIP事件模块
- swift-5类型别名和布尔值
- 线程模型的综述
- Linux 安装PIL库
- Sofia-SIP辅助文档十二 - Sofia SIP用户代理库 - "iptsec" - 认证模块
- ESP8266-12 wifi芯片使用教程1——串口输出Hello world