PAT_1007
来源:互联网 发布:apache主目录配置文件 编辑:程序博客网 时间:2024/06/06 23:19
1007. 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 ;inline int max(int a , int b) {return (a > b) ? a : b ; }int main(){int last;int first;int *arr;int *b;int n;cin>>n;arr = new int[n];b = new int [n];for(int i=0;i<n;++i){cin>>arr[i];}b[0] = arr[0] ; int i , mam = b[0]; last = 0;bool mark= (arr[0]>=0);for(i = 1; i < n ; ++i){ if(arr[i] >= 0)mark = true;b[i] = max(arr[i] , b[i-1] + arr[i]); if(mam < b[i]){mam = b[i] ; last = i;}}if(mark == false){mam = 0;first = 0;last = n-1;}else{int temp=mam;int tl=last;while(temp!=0){temp-=arr[tl];--tl;}first = tl+1;}cout<<mam<<" "<<arr[first]<<" "<<arr[last];return 0 ; }
- PAT_1007
- pat_1007
- PAT_1007: Maximum Subsequence Sum
- PAT_1007.Maximum Subsequence Sum
- PAT_1007. 素数对猜想 (20)
- 斐波那契数列
- 大神oracle体系结构详解
- 双向链表的实现
- resin 入门笔记
- Firefox插件中invoke方法获取参数以及参数转化
- PAT_1007
- Java线程池使用说明
- xp 权限设置
- 建立嵌入式gdb调试环境
- ios 实现推送消息
- android 4.4 shareactionprovider小结
- WTL在Win8.1系统WM_DROPFILES无法响应的解决办法
- IO系统性能之一:衡量性能的几个指标
- POJ 1007