1007. Maximum Subsequence Sum (25)
2015-12-06 09:57
573 查看
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:[/b]
来源: <http://www.patest.cn/contests/pat-a-practise/1007>
[/code]
来自为知笔记(Wiz)
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:[/b]
10 -10 1 2 3 4 -5 -23 3 7 -21Sample Output:[/b]
10 1 4
来源: <http://www.patest.cn/contests/pat-a-practise/1007>
#include <iostream>
#include <vector>
using namespace std;
vector<int> num;
int main(void) {
int n;
cin >> n;
int negCnt = 0;
for (int i = 0; i < n; i++) {
int itemp;
cin >> itemp;
if (itemp < 0)
negCnt++;
num.push_back(itemp);
}
if (negCnt == n) {
cout << "0 " << num[0] << " " << num[n - 1];
return 0;
}
int max = 0;
int max_start = 0, max_end = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
//sum = num[i];
sum = 0;
for (int j = i; j < n; j++) {
sum += num[j];
if (sum > max) {
max = sum;
max_start = num[i];
max_end = num[j];
}
}
}
cout << max << " " << max_start << " " << max_end;
return 0;
}
[/code]
来自为知笔记(Wiz)
相关文章推荐
- EasyUI中文帮助文档
- Avro技术应用_7. 将 Avro 数据读写到一个 Parquet 文件中 -- 待完善
- Hadoop中数据序列化的常用方式:SequenceFile, Avro, Thrift, ProtoBuff -- 待完善
- Ubiquitous Religions
- mvc项目架构搭建之UI层的搭建
- aJax提交——服务端不能用request存储数据,session存数据客户端可以接收到
- LibGDX_1.6: 纹理(图片)的绘制 与 assets 资源文件夹: UI 初体验
- UESTC 1253 阿里巴巴和n个大盗 博弈、策略
- server core & GUI
- UESTC 1255 斓少摘苹果 贪心法
- 谈谈UICollectionView添加头视图的方法
- 1041. Be Unique (20)【水题】——PAT (Advanced Level) Practise
- How to build openmcu-ru on ubuntu
- UESTC 1251 谕神的密码 贪心法
- UESTC 1256 昊昊喜欢运动 n^2的预处理 or 前缀和
- Ubuntu 14 Trusty安装hue
- request.getParameterMap()使用方法
- 用XDOCBuilder制作精美的EPUB
- 如何避免UI界面中下拉框ComboBox文本项的硬编码?
- 基于swing的UI原型辅助设计