01-复杂度2. Maximum Subsequence Sum (25)
来源:互联网 发布:java集合框架面试题 编辑:程序博客网 时间:2024/06/02 07:31
01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. TheMaximum 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
结果:有一个测试点没通过。。。。。。
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. TheMaximum 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
#include <iostream> #include <stdio.h> #include <iomanip> using namespace std;struct max{int max;int start;int end;}m; struct max maxSubSum2(int A[] ,int N) { // int thisSum = 0 , maxSum =0; // int i,j; // for (int i = 0; i < N; i++) // { // int thisSum = 0; // for (int j = i; j < N; j++) // { // thisSum += A[j]; // if (thisSum > maxSum) // maxSum = thisSum; // } // } int sum = 0;m.max = A[0];m.start = 0;m.end = 0;int i;int j = 0;if(A[0] > 0){sum = A[0];m.start = 0;}else{m.start = 1;m.end = 1;j = 1;}for(i = 1; i < N; i++){sum += A[i];if(sum > m.max){m.max = sum;m.end = i;m.start = j;}if(sum < 0){sum = 0;j = i + 1;}} return m; } int main(){ int n=0; scanf("%d",&n); int *number=new int[n]; for(int i=0;i<n;++i) scanf("%d ",&number[i]); // int maxsum = 0; m = maxSubSum2(number,n); cout<<m.max<<" "<<number[m.start]<<" "<<number[m.end]<<endl;// cout<<m.max<<" "<<m.start<<" "<<m.end<<endl; }
结果:有一个测试点没通过。。。。。。
0 0
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2. Maximum Subsequence Sum (25)
- 01-复杂度2.Maximum Subsequence Sum
- PAT: 01-复杂度2. Maximum Subsequence Sum
- 01-复杂度2. Maximum Subsequence Sum
- powerdesigner导出sql
- 杭电2112(SPFA)
- Apache HTTP Server 2.4 绿色版 安装成系统服务
- 如何将一个十进制数字转化为2进制(n进制,以2进制为例)
- openwrt mjpeg-stream使用
- 01-复杂度2. Maximum Subsequence Sum (25)
- BAT及各大互联网公司2014前端笔试面试题--Html,Css篇
- 根据线程处理Mysql的死锁问题
- JOptionPane类提示框的一些常用的方法
- Effective Objective-C 2.0 第3条:多用字面量语法,少用与之等价的方法
- 【iOS开发技术】NSPredicate谓词的用法 数组过滤
- 网络流(dinic算法)
- 微信错误提示 code = -4/ 微信发送被拒绝
- iOS项目开发实战——制作View的颜色渐变动画