博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT 1007
阅读量:6086 次
发布时间:2019-06-20

本文共 2171 字,大约阅读时间需要 7 分钟。

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 -21
Sample Output:
10 1 4

动态规划的经典问题,最大连续子序列和。

 

代码

 1 #include <stdio.h>
 2 
 3 
int main()
 4 {
 5     
int K;
 6     
int data[
10000];
 7     
int i;
 8     
int flag;
 9     
while(scanf(
"
%d
",&K) != EOF){
10         flag = 
0;
11         
for (i=
0;i<K;++i){
12             scanf(
"
%d
",&data[i]);
13             
if(data[i] >= 
0)
14                 flag = 
1;
15         }
16         
if(flag == 
0){
17             printf(
"
0 %d %d\n
",data[
0],data[K-
1]);
18             
continue;
19         }
20         
int max_seq = data[
0];
21         
int max_s = 
0,max_e = 
0;
22         
int sum = data[
0];
23         
int sum_s = 
0,sum_e = 
0;
24         
for(i=
1;i<K;++i){
25             
if(sum < 
0){
26                 sum = data[i];
27                 sum_s = sum_e = i;
28             }
29             
else{
30                 sum += data[i];
31                 sum_e = i;
32             }
33             
if(sum > max_seq){
34                 max_seq = sum;
35                 max_s = sum_s;
36                 max_e = sum_e;
37             }
38         }
39         printf(
"
%d %d %d\n
",max_seq,data[max_s],data[max_e]);
40     }
41     
return 
0;
42 }

 

posted on
2014-02-10 22:07 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/boostable/p/pat_1007.html

你可能感兴趣的文章
没有显示器的情况下安装和使用树莓派
查看>>
【android】使用handler更新UI
查看>>
mochiweb 源码阅读(十五)
查看>>
前端面试中的常见的算法问题
查看>>
计算机语言的基本理论
查看>>
nodejs流之行读取器例子
查看>>
批量文件重命名工具
查看>>
简单说一下UWP中的JumpList
查看>>
unity将object[]或者string对象转换成枚举enum
查看>>
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 9 章 函数和操作符_9.19. 范围函数和操作符...
查看>>
以太坊系列之六: p2p模块--以太坊源码学习
查看>>
使用scikit-learn解决文本多分类问题(附python演练)
查看>>
2018 年最值得关注的 JavaScript 趋势
查看>>
什么是区块链?超级账本 Brian Behlendorf 从五个方面教你认识
查看>>
Linux中的帮助功能
查看>>
针对Android的Pegasus恶意软件版本和针对iOS的有什么不同?
查看>>
全局探色器
查看>>
Hive Export和Import介绍及操作示例
查看>>
http://mongoexplorer.com/ 一个不错的 mongodb 客户端工具。。。
查看>>
Xcode 4.3 使用xcodebuild命令编译项目环境设置
查看>>