PAT (Advanced Level) Practise 1007. Maximum Subsequence Sum (25)
来源:互联网 发布:数据库逻辑完整性 编辑:程序博客网 时间:2024/06/16 14:09
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 <cstdio>#include <algorithm>using namespace std;const int maxn=10000+10;int a[maxn],dp[maxn],s[maxn]={0};int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&a[i]); int ok=0; for(int i=0;i<n;i++){ if(a[i]>=0) ok=1; } if(!ok){ printf("0 %d %d\n",a[0],a[n-1]); return 0; } dp[0]=a[0]; for(int i=1;i<n;i++){ if(dp[i-1]+a[i]>a[i]){ dp[i]=dp[i-1]+a[i]; s[i]=s[i-1]; } else{ dp[i]=a[i]; s[i]=i; } } int k=0; for(int i=1;i<n;i++){ if(dp[i]>dp[k]) k=i; } printf("%d %d %d\n",dp[k],a[s[k]],a[k]); return 0;}
- pat PAT (Advanced Level) Practise 1007. Maximum Subsequence Sum (25)
- PAT (Advanced Level) Practise 1007. Maximum Subsequence Sum (25)
- PAT (Advanced Level) Practise 1007. Maximum Subsequence Sum (25)
- PAT (Advanced Level) Practise 1007. Maximum Subsequence Sum (25)
- PAT (Advanced Level) Practise 1007 Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1007 Maximum Subsequence Sum
- PAT(Advanced level) 1007. Maximum Subsequence Sum*
- 【PAT Advanced Level】1007. Maximum Subsequence Sum (25)
- 【c++】PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)*
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 解题报告
- 【PAT】【Advanced Level】1007. Maximum Subsequence Sum (25)
- PAT Advanced Level 1007:Maximum Subsequence Sum
- PAT (Advanced) 1007. Maximum Subsequence Sum (25)
- [PAT (Advanced Level) ]1007. Maximum Subsequence Sum 解题文档
- 浙大 PAT Advanced level 1007. Maximum Subsequence Sum
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 最大连续子数组,一次遍历,stack辅助
- Pat(Advanced Level)Practice--1007(Maximum Subsequence Sum)
- 不规则数组
- MySQL在CenterOS和Ubuntu的安装
- android.content.ActivityNotFoundException: No Activity found to handle Intent
- 静态资源运维方案
- HBase的下载、安装与配置
- PAT (Advanced Level) Practise 1007. Maximum Subsequence Sum (25)
- 闲得蛋疼之MC9S12单片机 纯汇编文件编写 与c混合
- 使用Intellij IDEA生成JavaDoc
- windows上Jenkins安装
- Android 优雅的为RecyclerView添加HeaderView和FooterView
- 获取微信小程序二维码,返回一堆乱码后,该怎么办?
- 读写csv文件
- Rxjava编程思想
- Android监听系统输入法键盘弹出显示与隐藏事件