01-2. Maximum Subsequence Sum (25)
来源:互联网 发布:csmar数据库怎么用 编辑:程序博客网 时间:2024/06/08 05:16
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. 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 -21Sample Output:
10 1 4
#include<iostream>using namespace std;int main(){int i,j,K,Flag=0,First=0,Last=0,temp=0;int c[10000];int Thissum,Maxsum;//freopen("C:\\Documents and Settings\\Administrator\\桌面\\cin.txt","r",stdin);cin>>K;for(i=0;i<K;i++){cin>>c[i];if(c[i]>=0)temp=1;//输入是否全是非负数的标志}Thissum=c[0];Maxsum=c[0];for(j=1;j<K;j++){if(Thissum>=0)Thissum+=c[j];else{Thissum=c[j];Flag=j;//最大子列和的第一个数标记}if(Thissum>Maxsum){Maxsum=Thissum;First=Flag;Last=j;}}if(temp==0)cout<<0<<" "<<c[0]<<" "<<c[K-1]<<endl;elsecout<<Maxsum<<" "<<c[First]<<" "<<c[Last]<<endl;return 0;}
- 01-2. Maximum Subsequence Sum (25)
- 01-2. Maximum Subsequence Sum (25)
- 01-2. Maximum Subsequence Sum (25)
- PAT 01-2. Maximum Subsequence Sum (25)
- PAT 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)
- Sql Varchar转换为Int
- NSTimer知识点和倒计时的实现
- hadoop 配置主机的SSH无密钥登陆
- SSH上传文件提示encoutered 2 errors during the transfer
- Responder一点也不神秘————iOS用户响应者链完全剖析
- 01-2. Maximum Subsequence Sum (25)
- A Pangram
- MBProgressHUD 源码解析
- hdu 3911 线段树+区间合并
- nodejs npm 常用命令
- SAP概览(初学者必看)
- Cocos2d-X的UI树(1)
- 写一个简单程序解析ELF
- ubuntu 用户添加 添加sudo权限 组添加 杂乱概述